home *** CD-ROM | disk | FTP | other *** search
/ Sprite 1984 - 1993 / Sprite 1984 - 1993.iso / src / cmds / ld / dist / RCS / ld.c,v < prev    next >
Encoding:
Text File  |  1990-10-26  |  150.5 KB  |  5,836 lines

  1. head     1.6;
  2. branch   ;
  3. access   ;
  4. symbols  ;
  5. locks    rab:1.6; strict;
  6. comment  @ * @;
  7.  
  8.  
  9. 1.6
  10. date     90.10.25.21.58.10;  author rab;  state Exp;
  11. branches ;
  12. next     1.5;
  13.  
  14. 1.5
  15. date     90.07.30.13.33.58;  author rab;  state Exp;
  16. branches ;
  17. next     1.4;
  18.  
  19. 1.4
  20. date     90.03.20.15.36.30;  author rab;  state Exp;
  21. branches ;
  22. next     1.3;
  23.  
  24. 1.3
  25. date     90.02.25.20.16.55;  author rab;  state Exp;
  26. branches ;
  27. next     1.2;
  28.  
  29. 1.2
  30. date     90.02.16.13.48.51;  author rab;  state Exp;
  31. branches ;
  32. next     1.1;
  33.  
  34. 1.1
  35. date     90.01.07.07.14.05;  author rab;  state Exp;
  36. branches ;
  37. next     ;
  38.  
  39.  
  40. desc
  41. @@
  42.  
  43.  
  44. 1.6
  45. log
  46. @Changes for symm.
  47. @
  48. text
  49. @/* Linker `ld' for GNU
  50.    Copyright (C) 1988 Free Software Foundation, Inc.
  51.  
  52.    This program is free software; you can redistribute it and/or modify
  53.    it under the terms of the GNU General Public License as published by
  54.    the Free Software Foundation; either version 1, or (at your option)
  55.    any later version.
  56.  
  57.    This program is distributed in the hope that it will be useful,
  58.    but WITHOUT ANY WARRANTY; without even the implied warranty of
  59.    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  60.    GNU General Public License for more details.
  61.  
  62.    You should have received a copy of the GNU General Public License
  63.    along with this program; if not, write to the Free Software
  64.    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.  */
  65.  
  66. /* Written by Richard Stallman with some help from Eric Albert.
  67.    Set, indirect, and warning symbol features added by Randy Smith.  */
  68.  
  69. #include <ar.h>
  70. #include <stdio.h>
  71. #include <sys/types.h>
  72. #include <sys/stat.h>
  73. #include <sys/file.h>
  74. #ifndef sony_news
  75. #include <fcntl.h>
  76. #endif
  77.  
  78. #define NO_C_PLUS_PLUS
  79.  
  80. #define TARGET_SUN2             2
  81. #define TARGET_SUN3             3
  82. #define TARGET_SUN4             4
  83. #define TARGET_ALTOS            5
  84. #define TARGET_I386             6   
  85. #define TARGET_HPUX             7
  86. #define TARGET_SONY_NEWS        8
  87. #define TARGET_SEQUENT          9
  88. #define TARGET_VAX              10
  89.  
  90. /*
  91.  * If `TARGET_MACHINE' was not defined on the compiler command line,
  92.  * then set it equal to the host machine.
  93.  */
  94. #ifndef TARGET_MACHINE
  95. #if defined(sun) && defined(sparc)
  96. #define TARGET_MACHINE  TARGET_SUN4
  97. #endif
  98. #if defined(sun) && (defined(m68020) || defined(mc68020))
  99. #define TARGET_MACHINE  TARGET_SUN3
  100. #endif
  101. #if defined(sun) && (defined(m68010) || defined(mc68010))
  102. #define TARGET_MACHINE  TARGET_SUN2
  103. #endif
  104. #if defined(ALTOS)
  105. #define TARGET_MACHINE  TARGET_ALTOS
  106. #endif
  107. #if defined(hpux)
  108. #define TARGET_MACHINE  TARGET_HPUX
  109. #endif
  110. #if defined(is386)
  111. #define TARGET_MACHINE  TARGET_I386
  112. #endif
  113. #if defined(sony_news)
  114. #define TARGET_MACHINE  TARGET_SONY_NEWS
  115. #endif
  116. #if defined(sequent)
  117. #define TARGET_MACHINE  TARGET_SEQUENT
  118. #endif
  119. #if defined(is68k) && !defined(TARGET_MACHINE)
  120. #define TARGET_MACHINE  TARGET_68K
  121. #endif
  122. #endif
  123.  
  124. #ifndef TARGET_MACHINE
  125.     CANNOT COMPILE, NO TARGET MACHINE SPECIFIED
  126. #endif
  127.  
  128. #ifndef BIG_ENDIAN
  129. #define BIG_ENDIAN      4321
  130. #endif
  131. #ifndef LITTLE_ENDIAN
  132. #define LITTLE_ENDIAN   1234
  133. #endif
  134. #ifndef PDP_ENDIAN
  135. /* I think it is unlikely that anybody will try to cross compile
  136.  * to or from a pdp, so only big-endian and little-endian are supported. */
  137. #define PDP_ENDIAN   3412
  138. #endif
  139.  
  140. #if TARGET_MACHINE==TARGET_SUN4         || \
  141.     TARGET_MACHINE==TARGET_SUN3         || \
  142.     TARGET_MACHINE==TARGET_SUN2         || \
  143.     TARGET_MACHINE==TARGET_ALTOS        || \
  144.     TARGET_MACHINE==TARGET_HPUX         || \
  145.     TARGET_MACHINE==TARGET_SONY_NEWS    || \
  146.     TARGET_MACHINE==TARGET_68K
  147. #define TARGET_BYTE_ORDER       BIG_ENDIAN
  148. #endif
  149.  
  150. #if TARGET_MACHINE==TARGET_VAX          || \
  151.     TARGET_MACHINE==TARGET_I386         || \
  152.     TARGET_MACHINE==TARGET_SEQUENT
  153. #define TARGET_BYTE_ORDER       LITTLE_ENDIAN
  154. #endif
  155.  
  156. #if TARGET_MACHINE==TARGET_SUN3
  157. #define TARGET_PAGE_SIZE    0x2000
  158. #endif
  159.  
  160. #if defined(sparc)      || \
  161.     defined(m68000)     || \
  162.     defined(mc68000)    || \
  163.     defined(m68010)     || \
  164.     defined(mc68010)    || \
  165.     defined(m68020)     || \
  166.     defined(mc68020)    || \
  167.     defined(is68k)      || \
  168.     defined(hpux)       || \
  169.     defined(sony_news)
  170. #define HOST_BYTE_ORDER     BIG_ENDIAN
  171. #endif
  172.  
  173. #if defined(vax)        || \
  174.     defined(is386)      || \
  175.     defined(ds3100)     || \
  176.     defined(sequent)
  177. #define HOST_BYTE_ORDER     LITTLE_ENDIAN
  178. #endif
  179.  
  180. #if HOST_BYTE_ORDER != TARGET_BYTE_ORDER
  181. static void fix_byte_order();
  182. static void fix_exec_header_byte_order();
  183. static void fix_symbol_byte_order();
  184. static void target_to_host_reloc_byte_order();
  185. static void host_to_target_reloc_byte_order();
  186. static void fix_symbol_root_byte_order();
  187. #endif
  188.  
  189. #ifdef COFF_ENCAPSULATE
  190. #include "a.out.encap.h"
  191. #else
  192. #ifdef sprite
  193. #if TARGET_MACHINE==TARGET_SUN4
  194. #include <sun4.md/a.out.h>
  195. #elif TARGET_MACHINE==TARGET_SUN3
  196. #include <sun3.md/a.out.h>
  197. #elif TARGET_MACHINE==TARGET_SEQUENT
  198. #include <symm.md/a.out.h>
  199. #else
  200.     no a.out.h specified
  201. #endif
  202. #else
  203. #include <a.out.h>
  204. #endif
  205. #endif
  206.  
  207. #ifdef sprite
  208. #undef NEW_SEG_SIZE
  209. #if TARGET_MACHINE==TARGET_SUN4
  210. #define NEW_SEG_SIZE    0x40000
  211. #endif
  212. #if TARGET_MACHINE==TARGET_SUN3
  213. #define NEW_SEG_SIZE    0x20000
  214. #endif
  215. #endif  /* sprite */
  216.  
  217. #ifndef N_SET_MAGIC
  218. #define N_SET_MAGIC(exec, val)  ((exec).a_magic = val)
  219. #endif
  220.  
  221. /* If compiled with GNU C, use the built-in alloca */
  222. #ifdef __GNUC__
  223. #define alloca __builtin_alloca
  224. #endif
  225.  
  226. /* Always use the GNU version of debugging symbol type codes, if possible.  */
  227.  
  228. #include "stab.h"
  229. #define CORE_ADDR unsigned long    /* For symseg.h */
  230. #include "symseg.h"
  231.  
  232. #ifdef USG
  233. #include <string.h>
  234. #else
  235. #include <strings.h>
  236. #endif
  237.  
  238. /* Determine whether we should attempt to handle (minimally)
  239.    N_BINCL and N_EINCL.  */
  240.  
  241. #if defined (__GNU_STAB__) || defined (N_BINCL)
  242. #define HAVE_SUN_STABS
  243. #endif
  244.  
  245. #define min(a,b) ((a) < (b) ? (a) : (b))
  246.  
  247. /* Macro to control the number of undefined references printed */
  248. #define MAX_UREFS_PRINTED    10
  249.  
  250. /* Size of a page; obtained from the operating system.  */
  251.  
  252. int page_size;
  253.  
  254. /* Name this program was invoked by.  */
  255.  
  256. char *progname;
  257.  
  258. /* System dependencies */
  259.  
  260. /* Define this if names etext, edata and end should not start with `_'.  */
  261. /* #define nounderscore 1 */
  262.  
  263. /* Define NON_NATIVE if using BSD or pseudo-BSD file format on a system
  264.    whose native format is different.  */
  265. /* #define NON_NATIVE */
  266.  
  267. /* Define this to specify the default executable format.  */
  268.  
  269. #if TARGET_MACHINE==TARGET_HPUX
  270. #define DEFAULT_MAGIC NMAGIC  /* hpux bugs screw ZMAGIC */
  271. #endif
  272.  
  273. #ifndef DEFAULT_MAGIC
  274. #define DEFAULT_MAGIC ZMAGIC
  275. #endif
  276.  
  277. /* Ordinary 4.3bsd lacks these macros in a.out.h.  */
  278.  
  279. #ifndef N_TXTADDR
  280. #if TARGET_MACHINE==TARGET_VAX || TARGET_MACHINE==TARGET_SONY_NEWS
  281. #define N_TXTADDR(X) 0
  282. #endif
  283. #if TARGET_MACHINE==TARGET_SUN3 || TARGET_MACHINE==TARGET_SUN2
  284. #define N_TXTADDR(x)  (sizeof (struct exec))
  285. #endif
  286. #if TARGET_MACHINE==TARGET_SEQUENT
  287. #define    N_TXTADDR(x) (N_ADDRADJ(x))
  288. #endif
  289. #endif
  290.  
  291. #ifndef N_DATADDR
  292. #if TARGET_MACHINE==TARGET_VAX || TARGET_MACHINE==TARGET_SONY_NEWS
  293. #define N_DATADDR(x) \
  294.     (((x).a_magic==OMAGIC)? (N_TXTADDR(x)+(x).a_text) \
  295.     : (page_size+((N_TXTADDR(x)+(x).a_text-1) & ~(page_size-1))))
  296. #endif
  297. #if TARGET_MACHINE==TARGET_SUN3 || TARGET_MACHINE==TARGET_SUN2
  298. #define SEGMENT_SIZE 0x20000
  299. #define N_DATADDR(x) \
  300.     (((x).a_magic==OMAGIC)? (N_TXTADDR(x)+(x).a_text) \
  301.      : (SEGMENT_SIZE + ((N_TXTADDR(x)+(x).a_text-1) & ~(SEGMENT_SIZE-1))))
  302. #endif
  303. #if TARGET_MACHINE==TARGET_SEQUENT
  304. #define N_DATADDR(x) \
  305.     (((x).a_magic==OMAGIC)? (N_TXTADDR(x)+(x).a_text) \
  306.     : (page_size+(((x).a_text-1) & ~(page_size-1))))
  307. #endif
  308. #endif
  309.  
  310. /* Define how to initialize system-dependent header fields.  */
  311. #if TARGET_MACHINE==TARGET_SUN4
  312. #define INITIALIZE_HEADER \
  313.   {outheader.a_machtype = M_SPARC; outheader.a_toolversion = 1;}
  314. #endif
  315. #if TARGET_MACHINE==TARGET_SUN2
  316. #define INITIALIZE_HEADER outheader.a_machtype = M_68010
  317. #endif
  318. #if TARGET_MACHINE==TARGET_SUN3
  319. #define INITIALIZE_HEADER outheader.a_machtype = M_68020
  320. #endif
  321. #if TARGET_MACHINE==TARGET_ALTOS
  322. #define INITIALIZE_HEADER N_SET_MACHTYPE (outheader, M_68020)
  323. #endif
  324. #if TARGET_MACHINE==TARGET_HPUX
  325. #define INITIALIZE_HEADER N_SET_MACHTYPE (outheader, HP9000S200_ID)
  326. #endif
  327. #if TARGET_MACHINE==TARGET_I386
  328. #define INITIALIZE_HEADER N_SET_MACHTYPE (outheader, M_386)
  329. #endif
  330. #if TARGET_MACHINE==TARGET_68K
  331. #define INITIALIZE_HEADER outheader.a_machtype = 0;  
  332. /* This enables code to take care of an ugly hack in the ISI OS.
  333.    If a symbol beings with _$, then the object file is included only
  334.    if the rest of the symbol name has been referenced. */
  335. #define DOLLAR_KLUDGE
  336. #endif
  337. #if TARGET==TARGET_SEQUENT
  338. /* This is all stuff needed to make standalone binaries */
  339. static struct gdtbl gdt_filler = {
  340.     { 0x0000FFFF, 0x00CF9A00 },    /* code entry */
  341.     { 0x0000FFFF, 0x00CF9200 },    /* data entry */
  342.     { 0x00180017, 0x00000000 },    /* 6 byte descriptor */
  343. };
  344.  
  345. static char instr[] = {
  346.     0x67, 0x66, 0x0f, 0x01, 0x15, 0x30, 0x00, 0x00, 0x00,
  347.     0x0f, 0x20, 0xc3,
  348.     0x80, 0xcb, 0x01,
  349.     0x0f, 0x22, 0xc3,
  350.     0xea, 0x5b, 0x00, 0x08, 0x00,
  351.     0xbb, 0x10, 0x00, 0x00, 0x00,
  352.     0x8e, 0xdb,
  353.     0x8e, 0xd3,
  354.     0x8e, 0xc3,
  355.     0xff, 0x25, 0x6c, 0x00, 0x00, 0x00,
  356.     0x00, 0x00, 0x00, 0x00, };
  357. #endif   
  358.  
  359. /*
  360.  * Alloca include.
  361.  */
  362. #if defined(sun) && defined(sparc) && !defined(__GNUC__)
  363. #include "alloca.h"
  364. #endif
  365.  
  366. #ifndef L_SET
  367. #define L_SET 0
  368. #endif
  369.  
  370. /*
  371.  * Ok.  Following are the relocation information macros.  If your
  372.  * system cannot use the default set (below), you must define all of these:
  373.  
  374.  *   relocation_info: This must be typedef'd (or #define'd) to the type
  375.  * of structure that is stored in the relocation info section of your
  376.  * a.out files.  Often this is defined in the a.out.h for your system.
  377.  *
  378.  *   RELOC_ADDRESS (rval): Offset into the current section of the
  379.  * <whatever> to be relocated.  *Must be an lvalue*.
  380.  *
  381.  *   RELOC_EXTERN_P (rval):  Is this relocation entry based on an
  382.  * external symbol (1), or was it fully resolved upon entering the
  383.  * loader (0) in which case some combination of the value in memory
  384.  * (if RELOC_MEMORY_ADD_P) and the extra (if RELOC_ADD_EXTRA) contains
  385.  * what the value of the relocation actually was.  *Must be an lvalue*.
  386.  *
  387.  *   RELOC_TYPE (rval): For a non-external relocation, this is the
  388.  * segment to relocate for.
  389.  *
  390.  *   RELOC_SYMBOL (rval): For an external relocation, this is the
  391.  * index of its symbol in the symbol table.  *Must be an lvalue*.
  392.  *
  393.  *   RELOC_MEMORY_ADD_P (rval): This should be 1 if the final
  394.  * relocation value output here should be added to memory; 0, if the
  395.  * section of memory described should simply be set to the relocation
  396.  * value.
  397.  *
  398.  *   RELOC_MEMORY_ADD_P (rval): If this is nonzero, the value previously
  399.  * present in the memory location to be relocated is *added*
  400.  * to the relocation value, to produce the final result.
  401.  * Otherwise, the relocation value is stored in the memory location
  402.  * and the value previously found there is ignored.
  403.  * By default, this is always 1.
  404.  *
  405.  *   RELOC_MEMORY_SUB_P (rval): If this is nonzero, the value previously
  406.  * present in the memory location to be relocated is *subtracted*
  407.  * from the relocation value, to produce the final result.
  408.  * By default, this is always 0.
  409.  *
  410.  *   RELOC_ADD_EXTRA (rval): (Optional) This macro, if defined, gives
  411.  * an extra value to be added to the relocation value based on the
  412.  * individual relocation entry.  *Must be an lvalue if defined*.
  413.  *
  414.  *   RELOC_PCREL_P (rval): True if the relocation value described is
  415.  * pc relative.
  416.  *
  417.  *   RELOC_VALUE_RIGHTSHIFT (rval): Number of bits right to shift the
  418.  * final relocation value before putting it where it belongs.
  419.  *
  420.  *   RELOC_TARGET_SIZE (rval): log to the base 2 of the number of
  421.  * bytes of size this relocation entry describes; 1 byte == 0; 2 bytes
  422.  * == 1; 4 bytes == 2, and etc.  This is somewhat redundant (we could
  423.  * do everything in terms of the bit operators below), but having this
  424.  * macro could end up producing better code on machines without fancy
  425.  * bit twiddling.  Also, it's easier to understand/code big/little
  426.  * endian distinctions with this macro.
  427.  *
  428.  *   RELOC_TARGET_BITPOS (rval): The starting bit position within the
  429.  * object described in RELOC_TARGET_SIZE in which the relocation value
  430.  * will go.
  431.  *
  432.  *   RELOC_TARGET_BITSIZE (rval): How many bits are to be replaced
  433.  * with the bits of the relocation value.  It may be assumed by the
  434.  * code that the relocation value will fit into this many bits.  This
  435.  * may be larger than RELOC_TARGET_SIZE if such be useful.
  436.  *
  437.  *
  438.  *        Things I haven't implemented
  439.  *        ----------------------------
  440.  *
  441.  *    Values for RELOC_TARGET_SIZE other than 0, 1, or 2.
  442.  *
  443.  *    Pc relative relocation for External references.
  444.  *
  445.  *
  446.  */
  447.  
  448. #if TARGET_MACHINE==TARGET_SUN4
  449. /* Sparc (Sun 4) macros */
  450. #undef relocation_info
  451. #define relocation_info                    reloc_info_sparc
  452. #define RELOC_ADDRESS(r)        ((r)->r_address)                 
  453. #define RELOC_EXTERN_P(r)               ((r)->r_extern)      
  454. #define RELOC_TYPE(r)                   ((r)->r_index)  
  455. #define RELOC_SYMBOL(r)                 ((r)->r_index)   
  456. #define RELOC_MEMORY_SUB_P(r)        0
  457. #define RELOC_MEMORY_ADD_P(r)           0
  458. #define RELOC_ADD_EXTRA(r)              ((r)->r_addend)       
  459. #define RELOC_PCREL_P(r)                \
  460.     ((int) (r)->r_type >= (int) RELOC_DISP8 && \
  461.      (int) (r)->r_type <= (int) RELOC_WDISP22)
  462. #define RELOC_VALUE_RIGHTSHIFT(r) \
  463.     (reloc_target_rightshift[(int) (r)->r_type])
  464. #define RELOC_TARGET_SIZE(r)            (reloc_target_size[(int) (r)->r_type])
  465. #define RELOC_TARGET_BITPOS(r)          0
  466. #define RELOC_TARGET_BITSIZE(r) \
  467.     (reloc_target_bitsize[(int) (r)->r_type])
  468.  
  469. /* Note that these are very dependent on the order of the enums in
  470.    enum reloc_type (in a.out.h); if they change the following must be
  471.    changed */
  472. /* Also note that the last few may be incorrect; I have no information */
  473. static int reloc_target_rightshift[] = {
  474.   0, 0, 0, 0, 0, 0, 2, 2, 10, 0, 0, 0, 0, 0, 0,
  475. };
  476. static int reloc_target_size[] = {
  477.   0, 1, 2, 0, 1, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
  478. };
  479. static int reloc_target_bitsize[] = {
  480.   8, 16, 32, 8, 16, 32, 30, 22, 22, 22, 13, 10, 32, 32, 16,
  481. };
  482.  
  483. #define    MAX_ALIGNMENT    (sizeof (double))
  484.   /* The pagesize on an old sun4 is 0x2000, on sparcStation it is 0x1000.
  485.      If you want to use the same binaries on both, then you need to use
  486.      the larger pagesize. */
  487. #define TARGET_PAGE_SIZE    0x2000
  488. #endif
  489.  
  490. #if TARGET_MACHINE==TARGET_SEQUENT
  491. #define RELOC_ADDRESS(r)        ((r)->r_address)
  492. #define RELOC_EXTERN_P(r)        ((r)->r_extern)
  493. #define RELOC_TYPE(r)        ((r)->r_symbolnum)
  494. #define RELOC_SYMBOL(r)        ((r)->r_symbolnum)
  495. #define RELOC_MEMORY_SUB_P(r)    ((r)->r_bsr)
  496. #define RELOC_MEMORY_ADD_P(r)    1
  497. #undef RELOC_ADD_EXTRA
  498. #define RELOC_PCREL_P(r)        ((r)->r_pcrel || (r)->r_bsr)
  499. #define RELOC_VALUE_RIGHTSHIFT(r)    0
  500. #define RELOC_TARGET_SIZE(r)        ((r)->r_length)
  501. #define RELOC_TARGET_BITPOS(r)    0
  502. #define RELOC_TARGET_BITSIZE(r)    32
  503. #define TARGET_PAGE_SIZE    0x1000
  504. #endif
  505.  
  506. /* Default macros */
  507. #ifndef RELOC_ADDRESS
  508. #define RELOC_ADDRESS(r)        ((r)->r_address)
  509. #define RELOC_EXTERN_P(r)        ((r)->r_extern)
  510. #define RELOC_TYPE(r)        ((r)->r_symbolnum)
  511. #define RELOC_SYMBOL(r)        ((r)->r_symbolnum)
  512. #define RELOC_MEMORY_SUB_P(r)    0
  513. #define RELOC_MEMORY_ADD_P(r)    1
  514. #undef RELOC_ADD_EXTRA
  515. #define RELOC_PCREL_P(r)        ((r)->r_pcrel)
  516. #define RELOC_VALUE_RIGHTSHIFT(r)    0
  517. #define RELOC_TARGET_SIZE(r)        ((r)->r_length)
  518. #define RELOC_TARGET_BITPOS(r)    0
  519. #define RELOC_TARGET_BITSIZE(r)    32
  520. #endif
  521.  
  522. #ifndef MAX_ALIGNMENT
  523. #define    MAX_ALIGNMENT    (sizeof (int))
  524. #endif
  525.  
  526. #ifdef nounderscore
  527. #define LPREFIX '.'
  528. #else
  529. #define LPREFIX 'L'
  530. #endif
  531.  
  532.  
  533. /* Special global symbol types understood by GNU LD.  */
  534.  
  535. /* The following type indicates the definition of a symbol as being
  536.    an indirect reference to another symbol.  The other symbol
  537.    appears as an undefined reference, immediately following this symbol.
  538.  
  539.    Indirection is asymmetrical.  The other symbol's value will be used
  540.    to satisfy requests for the indirect symbol, but not vice versa.
  541.    If the other symbol does not have a definition, libraries will
  542.    be searched to find a definition.
  543.  
  544.    So, for example, the following two lines placed in an assembler
  545.    input file would result in an object file which would direct gnu ld
  546.    to resolve all references to symbol "foo" as references to symbol
  547.    "bar". 
  548.  
  549.     .stabs "_foo",11,0,0,0
  550.     .stabs "_bar",1,0,0,0
  551.  
  552.    Note that (11 == (N_INDR | N_EXT)) and (1 == (N_UNDF | N_EXT)).  */
  553.  
  554. #ifndef N_INDR
  555. #define N_INDR 0xa
  556. #endif
  557.  
  558. /* The following symbols refer to set elements.  These are expected
  559.    only in input to the loader; they should not appear in loader
  560.    output (unless relocatable output is requested).  To be recognized
  561.    by the loader, the input symbols must have their N_EXT bit set.
  562.    All the N_SET[ATDB] symbols with the same name form one set.  The
  563.    loader collects all of these elements at load time and outputs a
  564.    vector for each name.
  565.    Space (an array of 32 bit words) is allocated for the set in the
  566.    data section, and the n_value field of each set element value is
  567.    stored into one word of the array.
  568.    The first word of the array is the length of the set (number of
  569.    elements).  The last word of the vector is set to zero for possible
  570.    use by incremental loaders.  The array is ordered by the linkage
  571.    order; the first symbols which the linker encounters will be first
  572.    in the array.
  573.  
  574.    In C syntax this looks like:
  575.  
  576.     struct set_vector {
  577.       unsigned int length;
  578.       unsigned int vector[length];
  579.       unsigned int always_zero;
  580.     };
  581.  
  582.    Before being placed into the array, each element is relocated
  583.    according to its type.  This allows the loader to create an array
  584.    of pointers to objects automatically.  N_SETA type symbols will not
  585.    be relocated.
  586.  
  587.    The address of the set is made into an N_SETV symbol
  588.    whose name is the same as the name of the set.
  589.    This symbol acts like a N_DATA global symbol
  590.    in that it can satisfy undefined external references.
  591.  
  592.    For the purposes of determining whether or not to load in a library
  593.    file, set element definitions are not considered "real
  594.    definitions"; they will not cause the loading of a library
  595.    member.
  596.  
  597.    If relocatable output is requested, none of this processing is
  598.    done.  The symbols are simply relocated and passed through to the
  599.    output file.
  600.  
  601.    So, for example, the following three lines of assembler code
  602.    (whether in one file or scattered between several different ones)
  603.    will produce a three element vector (total length is five words;
  604.    see above), referenced by the symbol "_xyzzy", which will have the
  605.    addresses of the routines _init1, _init2, and _init3.
  606.  
  607.    *NOTE*: If symbolic addresses are used in the n_value field of the
  608.    defining .stabs, those symbols must be defined in the same file as
  609.    that containing the .stabs.
  610.  
  611.     .stabs "_xyzzy",23,0,0,_init1
  612.     .stabs "_xyzzy",23,0,0,_init2
  613.     .stabs "_xyzzy",23,0,0,_init3
  614.  
  615.    Note that (23 == (N_SETT | N_EXT)).  */
  616.  
  617. #ifndef N_SETA
  618. #define    N_SETA    0x14        /* Absolute set element symbol */
  619. #endif                /* This is input to LD, in a .o file.  */
  620.  
  621. #ifndef N_SETT
  622. #define    N_SETT    0x16        /* Text set element symbol */
  623. #endif                /* This is input to LD, in a .o file.  */
  624.  
  625. #ifndef N_SETD
  626. #define    N_SETD    0x18        /* Data set element symbol */
  627. #endif                /* This is input to LD, in a .o file.  */
  628.  
  629. #ifndef N_SETB
  630. #define    N_SETB    0x1A        /* Bss set element symbol */
  631. #endif                /* This is input to LD, in a .o file.  */
  632.  
  633. /* Macros dealing with the set element symbols defined in a.out.h */
  634. #define    SET_ELEMENT_P(x)    ((x)>=N_SETA&&(x)<=(N_SETB|N_EXT))
  635. #define TYPE_OF_SET_ELEMENT(x)    ((x)-N_SETA+N_ABS)
  636.  
  637. #ifndef N_SETV
  638. #define N_SETV    0x1C        /* Pointer to set vector in data area.  */
  639. #endif                /* This is output from LD.  */
  640.  
  641. /* If a this type of symbol is encountered, its name is a warning
  642.    message to print each time the symbol referenced by the next symbol
  643.    table entry is referenced.   
  644.  
  645.    This feature may be used to allow backwards compatibility with
  646.    certain functions (eg. gets) but to discourage programmers from
  647.    their use.
  648.  
  649.    So if, for example, you wanted to have ld print a warning whenever
  650.    the function "gets" was used in their C program, you would add the
  651.    following to the assembler file in which gets is defined:
  652.  
  653.     .stabs "Obsolete function \"gets\" referenced",30,0,0,0
  654.     .stabs "_gets",1,0,0,0
  655.  
  656.    These .stabs do not necessarily have to be in the same file as the
  657.    gets function, they simply must exist somewhere in the compilation.  */
  658.  
  659. #ifndef N_WARNING
  660. #define N_WARNING 0x1E        /* Warning message to print if symbol
  661.                    included */
  662. #endif                /* This is input to ld */
  663.  
  664. #ifndef __GNU_STAB__
  665.  
  666. /* Line number for the data section.  This is to be used to describe
  667.    the source location of a variable declaration.  */
  668. #ifndef N_DSLINE
  669. #define N_DSLINE (N_SLINE+N_DATA-N_TEXT)
  670. #endif
  671.  
  672. /* Line number for the bss section.  This is to be used to describe
  673.    the source location of a variable declaration.  */
  674. #ifndef N_BSLINE
  675. #define N_BSLINE (N_SLINE+N_BSS-N_TEXT)
  676. #endif
  677.  
  678. #endif /* not __GNU_STAB__ */
  679.  
  680. /* Symbol table */
  681.  
  682. /* Global symbol data is recorded in these structures,
  683.    one for each global symbol.
  684.    They are found via hashing in 'symtab', which points to a vector of buckets.
  685.    Each bucket is a chain of these structures through the link field.  */
  686.  
  687. typedef
  688.   struct glosym
  689.     {
  690.       /* Pointer to next symbol in this symbol's hash bucket.  */
  691.       struct glosym *link;
  692.       /* Name of this symbol.  */
  693.       char *name;
  694.       /* Value of this symbol as a global symbol.  */
  695.       long value;
  696.       /* Chain of external 'nlist's in files for this symbol, both defs
  697.      and refs.  */
  698.       struct nlist *refs;
  699.       /* Any warning message that might be associated with this symbol
  700.          from an N_WARNING symbol encountered. */
  701.       char *warning;
  702.       /* Nonzero means definitions of this symbol as common have been seen,
  703.      and the value here is the largest size specified by any of them.  */
  704.       int max_common_size;
  705.       /* For relocatable_output, records the index of this global sym in the
  706.      symbol table to be written, with the first global sym given index 0.*/
  707.       int def_count;
  708.       /* Nonzero means a definition of this global symbol is known to exist.
  709.      Library members should not be loaded on its account.  */
  710.       char defined;
  711.       /* Nonzero means a reference to this global symbol has been seen
  712.      in a file that is surely being loaded.
  713.      A value higher than 1 is the n_type code for the symbol's
  714.      definition.  */
  715.       char referenced;
  716.       /* A count of the number of undefined references printed for a
  717.      specific symbol.  If a symbol is unresolved at the end of
  718.      digest_symbols (and the loading run is supposed to produce
  719.      relocatable output) do_file_warnings keeps track of how many
  720.      unresolved reference error messages have been printed for
  721.      each symbol here.  When the number hits MAX_UREFS_PRINTED,
  722.      messages stop. */
  723.       unsigned char undef_refs;
  724.       /* 1 means that this symbol has multiple definitions.  2 means
  725.          that it has multiple definitions, and some of them are set
  726.      elements, one of which has been printed out already.  */
  727.       unsigned char multiply_defined;
  728.       /* Nonzero means print a message at all refs or defs of this symbol */
  729.       char trace;
  730.     }
  731.   symbol;
  732.  
  733. #ifndef NO_C_PLUS_PLUS
  734. /* Demangler for C++. */
  735. extern char *cplus_demangle ();
  736. #endif /* NO_C_PLUS_PLUS */
  737.  
  738. /* Demangler function to use. */
  739. char *(*demangler)() = NULL;
  740.  
  741.  
  742. /* Number of buckets in symbol hash table */
  743. #define    TABSIZE    1009
  744.  
  745. /* The symbol hash table: a vector of TABSIZE pointers to struct glosym. */
  746. symbol *symtab[TABSIZE];
  747.  
  748. /* Number of symbols in symbol hash table. */
  749. int num_hash_tab_syms = 0;
  750.  
  751. /* Count the number of nlist entries that are for local symbols.
  752.    This count and the three following counts
  753.    are incremented as as symbols are entered in the symbol table.  */
  754. int local_sym_count;
  755.  
  756. /* Count number of nlist entries that are for local symbols
  757.    whose names don't start with L. */
  758. int non_L_local_sym_count;
  759.  
  760. /* Count the number of nlist entries for debugger info.  */
  761. int debugger_sym_count;
  762.  
  763. /* Count the number of global symbols referenced and not defined.  */
  764. int undefined_global_sym_count;
  765.  
  766. /* Count the number of global symbols multiply defined.  */
  767. int multiple_def_count;
  768.  
  769. /* Count the number of defined global symbols.
  770.    Each symbol is counted only once
  771.    regardless of how many different nlist entries refer to it,
  772.    since the output file will need only one nlist entry for it.
  773.    This count is computed by `digest_symbols';
  774.    it is undefined while symbols are being loaded. */
  775. int defined_global_sym_count;
  776.  
  777. /* Count the number of symbols defined through common declarations.
  778.    This count is kept in symdef_library, linear_library, and
  779.    enter_global_ref.  It is incremented when the defined flag is set
  780.    in a symbol because of a common definition, and decremented when
  781.    the symbol is defined "for real" (ie. by something besides a common
  782.    definition).  */
  783. int common_defined_global_count;
  784.  
  785. /* Count the number of set element type symbols and the number of
  786.    separate vectors which these symbols will fit into.  See the
  787.    GNU a.out.h for more info.
  788.    This count is computed by 'enter_file_symbols' */
  789. int set_symbol_count;
  790. int set_vector_count;
  791.  
  792. /* Define a linked list of strings which define symbols which should
  793.    be treated as set elements even though they aren't.  Any symbol
  794.    with a prefix matching one of these should be treated as a set
  795.    element.
  796.  
  797.    This is to make up for deficiencies in many assemblers which aren't
  798.    willing to pass any stabs through to the loader which they don't
  799.    understand.  */
  800. struct string_list_element {
  801.   char *str;
  802.   struct string_list_element *next;
  803. };
  804.  
  805. struct string_list_element *set_element_prefixes;
  806.  
  807. /* Count the number of definitions done indirectly (ie. done relative
  808.    to the value of some other symbol. */
  809. int global_indirect_count;
  810.  
  811. /* Count the number of warning symbols encountered. */
  812. int warning_count;
  813.  
  814. /* Total number of symbols to be written in the output file.
  815.    Computed by digest_symbols from the variables above.  */
  816. int nsyms;
  817.  
  818.  
  819. /* Nonzero means ptr to symbol entry for symbol to use as start addr.
  820.    -e sets this.  */
  821. symbol *entry_symbol;
  822.  
  823. symbol *edata_symbol;   /* the symbol _edata */
  824. symbol *etext_symbol;   /* the symbol _etext */
  825. symbol *end_symbol;    /* the symbol _end */
  826.  
  827. /* Each input file, and each library member ("subfile") being loaded,
  828.    has a `file_entry' structure for it.
  829.  
  830.    For files specified by command args, these are contained in the vector
  831.    which `file_table' points to.
  832.  
  833.    For library members, they are dynamically allocated,
  834.    and chained through the `chain' field.
  835.    The chain is found in the `subfiles' field of the `file_entry'.
  836.    The `file_entry' objects for the members have `superfile' fields pointing
  837.    to the one for the library.  */
  838.  
  839. struct file_entry {
  840.   /* Name of this file.  */
  841.   char *filename;
  842.   /* Name to use for the symbol giving address of text start */
  843.   /* Usually the same as filename, but for a file spec'd with -l
  844.      this is the -l switch itself rather than the filename.  */
  845.   char *local_sym_name;
  846.  
  847.   /* Describe the layout of the contents of the file */
  848.  
  849.   /* The file's a.out header.  */
  850.   struct exec header;
  851.   /* Offset in file of GDB symbol segment, or 0 if there is none.  */
  852.   int symseg_offset;
  853.  
  854.   /* Describe data from the file loaded into core */
  855.  
  856.   /* Symbol table of the file.  */
  857.   struct nlist *symbols;
  858.   /* Size in bytes of string table.  */
  859.   int string_size;
  860.   /* Pointer to the string table.
  861.      The string table is not kept in core all the time,
  862.      but when it is in core, its address is here.  */
  863.   char *strings;
  864.  
  865.   /* Next two used only if `relocatable_output' or if needed for */
  866.   /* output of undefined reference line numbers. */
  867.  
  868.   /* Text reloc info saved by `write_text' for `coptxtrel'.  */
  869.   struct relocation_info *textrel;
  870.   /* Data reloc info saved by `write_data' for `copdatrel'.  */
  871.   struct relocation_info *datarel;
  872.  
  873.   /* Relation of this file's segments to the output file */
  874.  
  875.   /* Start of this file's text seg in the output file core image.  */
  876.   int text_start_address;
  877.   /* Start of this file's data seg in the output file core image.  */
  878.   int data_start_address;
  879.   /* Start of this file's bss seg in the output file core image.  */
  880.   int bss_start_address;
  881.   /* Offset in bytes in the output file symbol table
  882.      of the first local symbol for this file.  Set by `write_file_symbols'.  */
  883.   int local_syms_offset;
  884.  
  885.   /* For library members only */
  886.  
  887.   /* For a library, points to chain of entries for the library members.  */
  888.   struct file_entry *subfiles;
  889.   /* For a library member, offset of the member within the archive.
  890.      Zero for files that are not library members.  */
  891.   int starting_offset;
  892.   /* Size of contents of this file, if library member.  */
  893.   int total_size;
  894.   /* For library member, points to the library's own entry.  */
  895.   struct file_entry *superfile;
  896.   /* For library member, points to next entry for next member.  */
  897.   struct file_entry *chain;
  898.  
  899.   /* 1 if file is a library. */
  900.   char library_flag;
  901.  
  902.   /* 1 if file's header has been read into this structure.  */
  903.   char header_read_flag;
  904.  
  905.   /* 1 means search a set of directories for this file.  */
  906.   char search_dirs_flag;
  907.  
  908.   /* 1 means this is base file of incremental load.
  909.      Do not load this file's text or data.
  910.      Also default text_start to after this file's bss. */
  911.   char just_syms_flag;
  912. };
  913.  
  914. /* Vector of entries for input files specified by arguments.
  915.    These are all the input files except for members of specified libraries.  */
  916. struct file_entry *file_table;
  917.  
  918. /* Length of that vector.  */
  919. int number_of_files;
  920.  
  921. /* When loading the text and data, we can avoid doing a close
  922.    and another open between members of the same library.
  923.  
  924.    These two variables remember the file that is currently open.
  925.    Both are zero if no file is open.
  926.  
  927.    See `each_file' and `file_close'.  */
  928.  
  929. struct file_entry *input_file;
  930. int input_desc;
  931.  
  932. /* The name of the file to write; "a.out" by default.  */
  933.  
  934. char *output_filename;
  935.  
  936. /* Descriptor for writing that file with `mywrite'.  */
  937.  
  938. int outdesc;
  939.  
  940. /* Header for that file (filled in by `write_header').  */
  941.  
  942. struct exec outheader;
  943.  
  944. #ifdef COFF_ENCAPSULATE
  945. struct coffheader coffheader;
  946. int need_coff_header;
  947. #endif
  948.  
  949. /* The following are computed by `digest_symbols'.  */
  950.  
  951. int text_size;        /* total size of text of all input files.  */
  952. int data_size;        /* total size of data of all input files.  */
  953. int bss_size;        /* total size of bss of all input files.  */
  954. int text_reloc_size;    /* total size of text relocation of all input files.  */
  955. int data_reloc_size;    /* total size of data relocation of all input */
  956.             /* files.  */
  957.  
  958. /* Specifications of start and length of the area reserved at the end
  959.    of the text segment for the set vectors.  Computed in 'digest_symbols' */
  960. int set_sect_start;
  961. int set_sect_size;
  962.  
  963. /* Pointer for in core storage for the above vectors, before they are
  964.    written. */
  965. unsigned long *set_vectors;
  966.  
  967. /* Amount of cleared space to leave between the text and data segments.  */
  968.  
  969. int text_pad;
  970.  
  971. /* Amount of bss segment to include as part of the data segment.  */
  972.  
  973. int data_pad;
  974.  
  975. /* Format of __.SYMDEF:
  976.    First, a longword containing the size of the 'symdef' data that follows.
  977.    Second, zero or more 'symdef' structures.
  978.    Third, a longword containing the length of symbol name strings.
  979.    Fourth, zero or more symbol name strings (each followed by a null).  */
  980.  
  981. struct symdef {
  982.   int symbol_name_string_index;
  983.   int library_member_offset;
  984. };
  985.  
  986. /* Record most of the command options.  */
  987.  
  988. /* Address we assume the text section will be loaded at.
  989.    We relocate symbols and text and data for this, but we do not
  990.    write any padding in the output file for it.  */
  991. int text_start;
  992.  
  993. /* Offset of default entry-pc within the text section.  */
  994. int entry_offset;
  995.  
  996. /* Address we decide the data section will be loaded at.  */
  997. int data_start;
  998.  
  999. /* `text-start' address is normally this much plus a page boundary.
  1000.    This is not a user option; it is fixed for each system.  */
  1001. int text_start_alignment;
  1002.  
  1003. /* Nonzero if -T was specified in the command line.
  1004.    This prevents text_start from being set later to default values.  */
  1005. int T_flag_specified;
  1006.  
  1007. /* Nonzero if -Tdata was specified in the command line.
  1008.    This prevents data_start from being set later to default values.  */
  1009. int Tdata_flag_specified;
  1010.  
  1011. /* Size to pad data section up to.
  1012.    We simply increase the size of the data section, padding with zeros,
  1013.    and reduce the size of the bss section to match.  */
  1014. int specified_data_size;
  1015.  
  1016. /* Gap to insert between end of bss and start of data seg */
  1017. int data_gap;
  1018.  
  1019. /* Nonzero if -Z was specified (for data_gap) */
  1020. int Z_flag_specified;
  1021.  
  1022. /* Magic number to use for the output file, set by switch.  */
  1023. int magic;
  1024.  
  1025. /* Nonzero means print names of input files as processed.  */
  1026. int trace_files;
  1027.  
  1028. /* Which symbols should be stripped (omitted from the output):
  1029.    none, all, or debugger symbols.  */
  1030. enum { STRIP_NONE, STRIP_ALL, STRIP_DEBUGGER } strip_symbols;
  1031.  
  1032. /* Which local symbols should be omitted:
  1033.    none, all, or those starting with L.
  1034.    This is irrelevant if STRIP_NONE.  */
  1035. enum { DISCARD_NONE, DISCARD_ALL, DISCARD_L } discard_locals;
  1036.  
  1037. /* 1 => write load map.  */
  1038. int write_map;
  1039.  
  1040. /* 1 => write relocation into output file so can re-input it later.  */
  1041. int relocatable_output;
  1042.  
  1043. /* 1 => assign space to common symbols even if `relocatable_output'.  */
  1044. int force_common_definition;
  1045.  
  1046. /* Standard directories to search for files specified by -l.  */
  1047. char *standard_search_dirs[] =
  1048. #ifdef STANDARD_SEARCH_DIRS
  1049.   {STANDARD_SEARCH_DIRS};
  1050. #else
  1051. #ifdef NON_NATIVE
  1052.   {"/usr/local/lib/gnu"};
  1053. #else
  1054.   {"/lib", "/usr/lib", "/usr/local/lib"};
  1055. #endif
  1056. #endif
  1057.  
  1058. /* Actual vector of directories to search;
  1059.    this contains those specified with -L plus the standard ones.  */
  1060. char **search_dirs;
  1061.  
  1062. /* Length of the vector `search_dirs'.  */
  1063. int n_search_dirs;
  1064.  
  1065. /* Non zero means to create the output executable. */
  1066. /* Cleared by nonfatal errors.  */
  1067. int make_executable;
  1068.  
  1069. /* Force the executable to be output, even if there are non-fatal
  1070.    errors */
  1071. int force_executable;
  1072.  
  1073. /* Keep a list of any symbols referenced from the command line (so
  1074.    that error messages for these guys can be generated). This list is
  1075.    zero terminated. */
  1076. struct glosym **cmdline_references;
  1077. int cl_refs_allocated;
  1078.  
  1079. void bcopy (), bzero ();
  1080. int malloc (), realloc ();
  1081. #ifndef alloca
  1082. int alloca ();
  1083. #endif
  1084. int free ();
  1085.  
  1086. int xmalloc ();
  1087. int xrealloc ();
  1088. void fatal ();
  1089. void fatal_with_file ();
  1090. void perror_name ();
  1091. void perror_file ();
  1092. void error ();
  1093.  
  1094. void digest_symbols ();
  1095. void print_symbols ();
  1096. void load_symbols ();
  1097. void decode_command ();
  1098. void list_undefined_symbols ();
  1099. void list_unresolved_references ();
  1100. void write_output ();
  1101. void write_header ();
  1102. void write_text ();
  1103. void read_file_relocation ();
  1104. void write_data ();
  1105. void write_rel ();
  1106. void write_syms ();
  1107. void write_symsegs ();
  1108. void mywrite ();
  1109. void symtab_init ();
  1110. void padfile ();
  1111. char *concat ();
  1112. char *get_file_name ();
  1113. symbol *getsym (), *getsym_soft ();
  1114.  
  1115. int
  1116. main (argc, argv)
  1117.      char **argv;
  1118.      int argc;
  1119. {
  1120. /*   Added this to stop ld core-dumping on very large .o files.    */
  1121. #ifdef RLIMIT_STACK
  1122.   /* Get rid of any avoidable limit on stack size.  */
  1123.   {
  1124.       struct rlimit rlim;
  1125.  
  1126.       /* Set the stack limit huge so that alloca does not fail. */
  1127.       getrlimit (RLIMIT_STACK, &rlim);
  1128.       rlim.rlim_cur = rlim.rlim_max;
  1129.       setrlimit (RLIMIT_STACK, &rlim);
  1130.   }
  1131. #endif /* RLIMIT_STACK */
  1132.  
  1133. #ifdef TARGET_PAGE_SIZE
  1134.   page_size = TARGET_PAGE_SIZE;
  1135. #else
  1136.   page_size = getpagesize ();
  1137. #endif
  1138.  
  1139.   progname = argv[0];
  1140.  
  1141.   /* Clear the cumulative info on the output file.  */
  1142.   text_size = 0;
  1143.   data_size = 0;
  1144.   bss_size = 0;
  1145.   text_reloc_size = 0;
  1146.   data_reloc_size = 0;
  1147.  
  1148.   data_pad = 0;
  1149.   text_pad = 0;
  1150.  
  1151.   /* Initialize the data about options.  */
  1152.  
  1153.   specified_data_size = 0;
  1154.   strip_symbols = STRIP_NONE;
  1155.   trace_files = 0;
  1156.   discard_locals = DISCARD_NONE;
  1157.   entry_symbol = 0;
  1158.   write_map = 0;
  1159.   relocatable_output = 0;
  1160.   force_common_definition = 0;
  1161.   T_flag_specified = 0;
  1162.   Tdata_flag_specified = 0;
  1163.   magic = DEFAULT_MAGIC;
  1164.   make_executable = 1;
  1165.   force_executable = 0;
  1166.   set_element_prefixes = 0;
  1167.   Z_flag_specified = 0;
  1168.  
  1169.   /* Initialize the cumulative counts of symbols.  */
  1170.  
  1171.   local_sym_count = 0;
  1172.   non_L_local_sym_count = 0;
  1173.   debugger_sym_count = 0;
  1174.   undefined_global_sym_count = 0;
  1175.   set_symbol_count = 0;
  1176.   set_vector_count = 0;
  1177.   global_indirect_count = 0;
  1178.   warning_count = 0;
  1179.   multiple_def_count = 0;
  1180.   common_defined_global_count = 0;
  1181.  
  1182.   /* Keep a list of symbols referenced from the command line */
  1183.   cl_refs_allocated = 10;
  1184.   cmdline_references =
  1185.     (struct glosym **) xmalloc (cl_refs_allocated
  1186.                   * sizeof(struct glosym *));
  1187.   *cmdline_references = 0;
  1188.  
  1189.   /* Completely decode ARGV.  */
  1190.  
  1191.   decode_command (argc, argv);
  1192.  
  1193.   /* Create the symbols `etext', `edata' and `end'.  */
  1194.  
  1195.   if (!relocatable_output)
  1196.     symtab_init ();
  1197.  
  1198.   /* Determine whether to count the header as part of
  1199.      the text size, and initialize the text size accordingly.
  1200.      This depends on the kind of system and on the output format selected.  */
  1201.  
  1202.   N_SET_MAGIC (outheader, magic);
  1203. #ifdef INITIALIZE_HEADER
  1204.   INITIALIZE_HEADER;
  1205. #endif
  1206.  
  1207.   text_size = sizeof (struct exec);
  1208. #ifdef COFF_ENCAPSULATE
  1209.   if (relocatable_output == 0)
  1210.     {
  1211.       need_coff_header = 1;
  1212.       /* set this flag now, since it will change the values of N_TXTOFF, etc */
  1213.       N_SET_FLAGS (outheader, N_FLAGS_COFF_ENCAPSULATE);
  1214.       text_size += sizeof (struct coffheader);
  1215.     }
  1216. #endif
  1217.  
  1218.   text_size -= N_TXTOFF (outheader);
  1219.  
  1220.   if (T_flag_specified)
  1221.       text_size = 0;    /* don't add sizeof(exec) at -T location */
  1222.  
  1223.   if (text_size < 0)
  1224.     text_size = 0;
  1225.   entry_offset = text_size;
  1226.  
  1227.   if (!T_flag_specified && !relocatable_output)
  1228.     text_start = N_TXTADDR (outheader);
  1229.  
  1230.   /* The text-start address is normally this far past a page boundary.  */
  1231.   text_start_alignment = text_start % page_size;
  1232.  
  1233.   /* Load symbols of all input files.
  1234.      Also search all libraries and decide which library members to load.  */
  1235.  
  1236.   load_symbols ();
  1237.  
  1238.   /* Compute where each file's sections go, and relocate symbols.  */
  1239.  
  1240.   digest_symbols ();
  1241.  
  1242.   /* Print error messages for any missing symbols, for any warning
  1243.      symbols, and possibly multiple definitions */
  1244.  
  1245.   do_warnings (stderr);
  1246.  
  1247.   /* Print a map, if requested.  */
  1248.  
  1249.   if (write_map) print_symbols (stdout);
  1250.  
  1251.   /* Write the output file.  */
  1252.  
  1253.   if (make_executable || force_executable)
  1254.     write_output ();
  1255.  
  1256.   exit (!make_executable);
  1257. }
  1258.  
  1259. void decode_option ();
  1260.  
  1261. /* Analyze a command line argument.
  1262.    Return 0 if the argument is a filename.
  1263.    Return 1 if the argument is a option complete in itself.
  1264.    Return 2 if the argument is a option which uses an argument.
  1265.  
  1266.    Thus, the value is the number of consecutive arguments
  1267.    that are part of options.  */
  1268.  
  1269. int
  1270. classify_arg (arg)
  1271.      register char *arg;
  1272. {
  1273.   if (*arg != '-') return 0;
  1274.   switch (arg[1])
  1275.     {
  1276.     case 'A':
  1277.     case 'D':
  1278.     case 'e':
  1279.     case 'L':
  1280.     case 'l':
  1281.     case 'o':
  1282.     case 'u':
  1283.     case 'V':
  1284.     case 'y':
  1285.     case 'Z':
  1286.       if (arg[2])
  1287.     return 1;
  1288.       return 2;
  1289.  
  1290.     case 'B':
  1291.       if (! strcmp (&arg[2], "static"))
  1292.     return 1;
  1293.  
  1294.     case 'T':
  1295.       if (arg[2] == 0)
  1296.     return 2;
  1297.       if (! strcmp (&arg[2], "text"))
  1298.     return 2;
  1299.       if (! strcmp (&arg[2], "data"))
  1300.     return 2;
  1301.       return 1;
  1302.  
  1303. #if TARGET==TARGET_SEQUENT      
  1304.       /* k == Symmetry standalone,
  1305.        * p == don't align text/data boundary (for 8K disk bootstraps).
  1306.        */
  1307.    case 'k':
  1308.    case 'p':
  1309.        return 1;
  1310. #endif       
  1311.     }
  1312.  
  1313.   return 1;
  1314. }
  1315.  
  1316. /* Process the command arguments,
  1317.    setting up file_table with an entry for each input file,
  1318.    and setting variables according to the options.  */
  1319.  
  1320. void
  1321. decode_command (argc, argv)
  1322.      char **argv;
  1323.      int argc;
  1324. {
  1325.   register int i;
  1326.   register struct file_entry *p;
  1327.  
  1328.   number_of_files = 0;
  1329.   output_filename = "a.out";
  1330.  
  1331.   n_search_dirs = 0;
  1332.   search_dirs = (char **) xmalloc (sizeof (char *));
  1333.  
  1334.   /* First compute number_of_files so we know how long to make file_table.  */
  1335.   /* Also process most options completely.  */
  1336.  
  1337.   for (i = 1; i < argc; i++)
  1338.     {
  1339.       register int code = classify_arg (argv[i]);
  1340.       if (code)
  1341.     {
  1342.       if (i + code > argc)
  1343.         fatal ("no argument following %s\n", argv[i]);
  1344.  
  1345.       decode_option (argv[i], argv[i+1]);
  1346.  
  1347.       if (argv[i][1] == 'l' || argv[i][1] == 'A')
  1348.         number_of_files++;
  1349.  
  1350.       i += code - 1;
  1351.     }
  1352.       else
  1353.     number_of_files++;
  1354.     }
  1355.  
  1356.   if (!number_of_files)
  1357.     fatal ("no input files", 0);
  1358.  
  1359.   p = file_table
  1360.     = (struct file_entry *) xmalloc (number_of_files * sizeof (struct file_entry));
  1361.   bzero (p, number_of_files * sizeof (struct file_entry));
  1362.  
  1363.   /* Now scan again and fill in file_table.  */
  1364.   /* All options except -A and -l are ignored here.  */
  1365.  
  1366.   for (i = 1; i < argc; i++)
  1367.     {
  1368.       register int code = classify_arg (argv[i]);
  1369.  
  1370.       if (code)
  1371.     {
  1372.       char *string;
  1373.       if (code == 2)
  1374.         string = argv[i+1];
  1375.       else
  1376.         string = &argv[i][2];
  1377.  
  1378.       if (argv[i][1] == 'A')
  1379.         {
  1380.           if (p != file_table)
  1381.         fatal ("-A specified before an input file other than the first");
  1382.  
  1383.           p->filename = string;
  1384.           p->local_sym_name = string;
  1385.           p->just_syms_flag = 1;
  1386.           p++;
  1387.         }
  1388.       if (argv[i][1] == 'l')
  1389.         {
  1390.           p->filename = concat ("lib", string, ".a");
  1391.           p->local_sym_name = concat ("-l", string, "");
  1392.           p->search_dirs_flag = 1;
  1393.           p++;
  1394.         }
  1395.       i += code - 1;
  1396.     }
  1397.       else
  1398.     {
  1399.       p->filename = argv[i];
  1400.       p->local_sym_name = argv[i];
  1401.       p++;
  1402.     }
  1403.     }
  1404.  
  1405.   /* Now check some option settings for consistency.  */
  1406.  
  1407. #ifndef sprite
  1408. #ifdef NMAGIC
  1409.   if ((magic == ZMAGIC || magic == NMAGIC)
  1410. #else
  1411. #ifdef SMAGIC
  1412.     if ((magic == SMAGIC || magic == ZMAGIC)
  1413. #else
  1414.     if ((magic == ZMAGIC)
  1415. #endif /* SMAGIC */
  1416. #endif
  1417.       && (text_start - text_start_alignment) & (page_size - 1))
  1418.     fatal ("-T argument not multiple of page size, with sharable output", 0);
  1419. #endif
  1420.  
  1421.   /* Append the standard search directories to the user-specified ones.  */
  1422.   {
  1423.     int n = sizeof standard_search_dirs / sizeof standard_search_dirs[0];
  1424.     n_search_dirs += n;
  1425.     search_dirs
  1426.       = (char **) xrealloc (search_dirs, n_search_dirs * sizeof (char *));
  1427.     bcopy (standard_search_dirs, &search_dirs[n_search_dirs - n],
  1428.        n * sizeof (char *));
  1429.   }
  1430. }
  1431.  
  1432.  
  1433. void
  1434. add_cmdline_ref (sp)
  1435.      struct glosym *sp;
  1436. {
  1437.   struct glosym **ptr;
  1438.  
  1439.   for (ptr = cmdline_references;
  1440.        ptr < cmdline_references + cl_refs_allocated && *ptr;
  1441.        ptr++)
  1442.     ;
  1443.  
  1444.   if (ptr >= cmdline_references + cl_refs_allocated - 1)
  1445.     {
  1446.       int diff = ptr - cmdline_references;
  1447.       
  1448.       cl_refs_allocated *= 2;
  1449.       cmdline_references = (struct glosym **)
  1450.     xrealloc (cmdline_references,
  1451.          cl_refs_allocated * sizeof (struct glosym *));
  1452.       ptr = cmdline_references + diff;
  1453.     }
  1454.   
  1455.   *ptr++ = sp;
  1456.   *ptr = (struct glosym *) 0;
  1457. }
  1458.     
  1459. int
  1460. set_element_prefixed_p (name)
  1461.      char *name;
  1462. {
  1463.   struct string_list_element *p;
  1464.   int i;
  1465.  
  1466.   for (p = set_element_prefixes; p; p = p->next)
  1467.     {
  1468.       for (i = 0; p->str[i] != '\0' && (p->str[i] == name[i]); i++)
  1469.     ;
  1470.  
  1471.       if (p->str[i] == '\0')
  1472.     return 1;
  1473.     }
  1474.   return 0;
  1475. }
  1476.  
  1477. int parse ();
  1478.  
  1479. /* Record an option and arrange to act on it later.
  1480.    ARG should be the following command argument,
  1481.    which may or may not be used by this option.
  1482.  
  1483.    The `l' and `A' options are ignored here since they actually
  1484.    specify input files.  */
  1485.  
  1486. void
  1487. decode_option (swt, arg)
  1488.      register char *swt, *arg;
  1489. {
  1490.   /* We get Bstatic from gcc on suns.  */
  1491.   if (! strcmp (swt + 1, "Bstatic"))
  1492.     return;
  1493.   if (! strcmp (swt + 1, "Ttext"))
  1494.     {
  1495. #ifdef sprite 
  1496.       magic = OMAGIC;
  1497. #endif
  1498.       text_start = parse (arg, "%x", "invalid argument to -Ttext");
  1499.       T_flag_specified = 1;
  1500.       return;
  1501.     }
  1502.   if (! strcmp (swt + 1, "Tdata"))
  1503.     {
  1504.       data_start = parse (arg, "%x", "invalid argument to -Tdata");
  1505.       Tdata_flag_specified = 1;
  1506.       return;
  1507.     }
  1508.   if (! strcmp (swt + 1, "noinhibit-exec"))
  1509.     {
  1510.       force_executable = 1;
  1511.       return;
  1512.     }
  1513.  
  1514.   if (swt[2] != 0)
  1515.     arg = &swt[2];
  1516.  
  1517.   switch (swt[1])
  1518.     {
  1519.     case 'A':
  1520.       return;
  1521.  
  1522.     case 'D':
  1523.       specified_data_size = parse (arg, "%x", "invalid argument to -D");
  1524.       return;
  1525.  
  1526.     case 'd':
  1527.       force_common_definition = 1;
  1528.       return;
  1529.  
  1530.     case 'e':
  1531.       entry_symbol = getsym (arg);
  1532.       if (!entry_symbol->defined && !entry_symbol->referenced)
  1533.     undefined_global_sym_count++;
  1534.       entry_symbol->referenced = 1;
  1535.       add_cmdline_ref (entry_symbol);
  1536.       return;
  1537.  
  1538. #if TARGET==TARGET_SEQUENT
  1539.     case 'k':
  1540. #ifdef SMAGIC
  1541.       magic = SMAGIC;
  1542. #else
  1543.       fatal("No SMAGIC defined for -k");
  1544. #endif
  1545.       return;
  1546. #endif
  1547.  
  1548.     case 'l':
  1549. #ifndef NO_C_PLUS_PLUS
  1550.       /* If linking with libg++, use the C++ demangler. */
  1551.       if (arg != NULL && strcmp (arg, "g++") == 0)
  1552.       demangler = cplus_demangle;
  1553. #endif /* NO_C_PLUS_PLUS */
  1554.       return;
  1555.  
  1556.     case 'L':
  1557.       n_search_dirs++;
  1558.       search_dirs
  1559.     = (char **) xrealloc (search_dirs, n_search_dirs * sizeof (char *));
  1560.       search_dirs[n_search_dirs - 1] = arg;
  1561.       return;
  1562.  
  1563.     case 'M':
  1564.       write_map = 1;
  1565.       return;
  1566.  
  1567.     case 'N':
  1568.       magic = OMAGIC;
  1569.       return;
  1570.  
  1571. #ifdef NMAGIC
  1572.     case 'n':
  1573.       magic = NMAGIC;
  1574.       return;
  1575. #endif
  1576.  
  1577.     case 'o':
  1578.       output_filename = arg;
  1579.       return;
  1580.  
  1581.     case 'r':
  1582.       relocatable_output = 1;
  1583.       magic = OMAGIC;
  1584.       text_start = 0;
  1585.       return;
  1586.  
  1587.     case 'S':
  1588.       strip_symbols = STRIP_DEBUGGER;
  1589.       return;
  1590.  
  1591.     case 's':
  1592.       strip_symbols = STRIP_ALL;
  1593.       return;
  1594.  
  1595.     case 'T':
  1596. #ifdef sprite
  1597.       magic = OMAGIC;
  1598. #endif      
  1599.       text_start = parse (arg, "%x", "invalid argument to -T");
  1600.       T_flag_specified = 1;
  1601.       return;
  1602.  
  1603.     case 't':
  1604.       trace_files = 1;
  1605.       return;
  1606.  
  1607.     case 'u':
  1608.       {
  1609.     register symbol *sp = getsym (arg);
  1610.     if (!sp->defined && !sp->referenced)
  1611.       undefined_global_sym_count++;
  1612.     sp->referenced = 1;
  1613.     add_cmdline_ref (sp);
  1614.       }
  1615.       return;
  1616.  
  1617.     case 'V':
  1618.       {
  1619.     struct string_list_element *new
  1620.       = (struct string_list_element *)
  1621.         xmalloc (sizeof (struct string_list_element));
  1622.  
  1623.     new->str = arg;
  1624.     new->next = set_element_prefixes;
  1625.     set_element_prefixes = new;
  1626.     return;
  1627.       }
  1628.  
  1629.     case 'X':
  1630.       discard_locals = DISCARD_L;
  1631.       return;
  1632.  
  1633.     case 'x':
  1634.       discard_locals = DISCARD_ALL;
  1635.       return;
  1636.  
  1637.     case 'y':
  1638.       {
  1639.     register symbol *sp = getsym (&swt[2]);
  1640.     sp->trace = 1;
  1641.       }
  1642.       return;
  1643.  
  1644.     case 'z':
  1645.       magic = ZMAGIC;
  1646.       return;
  1647.  
  1648.     case 'Z':
  1649.       data_gap = parse (arg, "%x", "invalid argument to -Z");
  1650.       if (data_gap % page_size)
  1651.       fatal("-Z argument not a multiple of page size", 0);
  1652.       Z_flag_specified = 1;
  1653.       return;
  1654.  
  1655.     default:
  1656.       fatal ("invalid command option `%s'", swt);
  1657.     }
  1658. }
  1659.  
  1660. /** Convenient functions for operating on one or all files being */
  1661.  /** loaded.  */
  1662. void print_file_name ();
  1663.  
  1664. /* Call FUNCTION on each input file entry.
  1665.    Do not call for entries for libraries;
  1666.    instead, call once for each library member that is being loaded.
  1667.  
  1668.    FUNCTION receives two arguments: the entry, and ARG.  */
  1669.  
  1670. void
  1671. each_file (function, arg)
  1672.      register void (*function)();
  1673.      register int arg;
  1674. {
  1675.   register int i;
  1676.  
  1677.   for (i = 0; i < number_of_files; i++)
  1678.     {
  1679.       register struct file_entry *entry = &file_table[i];
  1680.       if (entry->library_flag)
  1681.         {
  1682.       register struct file_entry *subentry = entry->subfiles;
  1683.       for (; subentry; subentry = subentry->chain)
  1684.         (*function) (subentry, arg);
  1685.     }
  1686.       else
  1687.     (*function) (entry, arg);
  1688.     }
  1689. }
  1690.  
  1691. /* Call FUNCTION on each input file entry until it returns a non-zero
  1692.    value.  Return this value.
  1693.    Do not call for entries for libraries;
  1694.    instead, call once for each library member that is being loaded.
  1695.  
  1696.    FUNCTION receives two arguments: the entry, and ARG.  It must be a
  1697.    function returning unsigned long (though this can probably be fudged). */
  1698. #if 0
  1699. unsigned long
  1700. check_each_file (function, arg)
  1701.      register unsigned long (*function)();
  1702.      register int arg;
  1703. {
  1704.   register int i;
  1705.   register unsigned long return_val;
  1706.  
  1707.   for (i = 0; i < number_of_files; i++)
  1708.     {
  1709.       register struct file_entry *entry = &file_table[i];
  1710.       if (entry->library_flag)
  1711.         {
  1712.       register struct file_entry *subentry = entry->subfiles;
  1713.       for (; subentry; subentry = subentry->chain)
  1714.         if (return_val = (*function) (subentry, arg))
  1715.           return return_val;
  1716.     }
  1717.       else
  1718.     if (return_val = (*function) (entry, arg))
  1719.       return return_val;
  1720.     }
  1721.   return 0;
  1722. }
  1723. #endif
  1724.  
  1725. /* Like `each_file' but ignore files that were just for symbol definitions.  */
  1726.  
  1727. void
  1728. each_full_file (function, arg)
  1729.      register void (*function)();
  1730.      register int arg;
  1731. {
  1732.   register int i;
  1733.  
  1734.   for (i = 0; i < number_of_files; i++)
  1735.     {
  1736.       register struct file_entry *entry = &file_table[i];
  1737.       if (entry->just_syms_flag)
  1738.     continue;
  1739.       if (entry->library_flag)
  1740.         {
  1741.       register struct file_entry *subentry = entry->subfiles;
  1742.       for (; subentry; subentry = subentry->chain)
  1743.         (*function) (subentry, arg);
  1744.     }
  1745.       else
  1746.     (*function) (entry, arg);
  1747.     }
  1748. }
  1749.  
  1750. /* Close the input file that is now open.  */
  1751.  
  1752. void
  1753. file_close ()
  1754. {
  1755.   close (input_desc);
  1756.   input_desc = 0;
  1757.   input_file = 0;
  1758. }
  1759.  
  1760. /* Open the input file specified by 'entry', and return a descriptor.
  1761.    The open file is remembered; if the same file is opened twice in a row,
  1762.    a new open is not actually done.  */
  1763.  
  1764. int
  1765. file_open (entry)
  1766.      register struct file_entry *entry;
  1767. {
  1768.   register int desc;
  1769.  
  1770.   if (entry->superfile)
  1771.     return file_open (entry->superfile);
  1772.  
  1773.   if (entry == input_file)
  1774.     return input_desc;
  1775.  
  1776.   if (input_file) file_close ();
  1777.  
  1778.   if (entry->search_dirs_flag)
  1779.     {
  1780.       int i;
  1781.  
  1782.       for (i = 0; i < n_search_dirs; i++)
  1783.     {
  1784.       register char *string
  1785.         = concat (search_dirs[i], "/", entry->filename);
  1786.       desc = open (string, O_RDONLY, 0);
  1787.       if (desc > 0)
  1788.         {
  1789.           entry->filename = string;
  1790.           entry->search_dirs_flag = 0;
  1791.           break;
  1792.         }
  1793.       free (string);
  1794.     }
  1795.     }
  1796.   else
  1797.     desc = open (entry->filename, O_RDONLY, 0);
  1798.  
  1799.   if (desc > 0)
  1800.     {
  1801.       input_file = entry;
  1802.       input_desc = desc;
  1803.       return desc;
  1804.     }
  1805.  
  1806.   perror_file (entry);
  1807.   /* NOTREACHED */
  1808. }
  1809.  
  1810. /* Print the filename of ENTRY on OUTFILE (a stdio stream),
  1811.    and then a newline.  */
  1812.  
  1813. void
  1814. prline_file_name (entry, outfile)
  1815.      struct file_entry *entry;
  1816.      FILE *outfile;
  1817. {
  1818.   print_file_name (entry, outfile);
  1819.   fprintf (outfile, "\n");
  1820. }
  1821.  
  1822. /* Print the filename of ENTRY on OUTFILE (a stdio stream).  */
  1823.  
  1824. void
  1825. print_file_name (entry, outfile)
  1826.      struct file_entry *entry;
  1827.      FILE *outfile;
  1828. {
  1829.   if (entry->superfile)
  1830.     {
  1831.       print_file_name (entry->superfile, outfile);
  1832.       fprintf (outfile, "(%s)", entry->filename);
  1833.     }
  1834.   else
  1835.     fprintf (outfile, "%s", entry->filename);
  1836. }
  1837.  
  1838. /* Return the filename of entry as a string (malloc'd for the purpose) */
  1839.  
  1840. char *
  1841. get_file_name (entry)
  1842.      struct file_entry *entry;
  1843. {
  1844.   char *result, *supfile;
  1845.   if (entry->superfile)
  1846.     {
  1847.       supfile = get_file_name (entry->superfile);
  1848.       result = (char *) xmalloc (strlen (supfile)
  1849.                  + strlen (entry->filename) + 3);
  1850.       sprintf (result, "%s(%s)", supfile, entry->filename);
  1851.       free (supfile);
  1852.     }
  1853.   else
  1854.     {
  1855.       result = (char *) xmalloc (strlen (entry->filename) + 1);
  1856.       strcpy (result, entry->filename);
  1857.     }
  1858.   return result;
  1859. }
  1860.  
  1861. /* Medium-level input routines for rel files.  */
  1862.  
  1863. /* Read a file's header into the proper place in the file_entry.
  1864.    DESC is the descriptor on which the file is open.
  1865.    ENTRY is the file's entry.  */
  1866.  
  1867. void
  1868. read_header (desc, entry)
  1869.      int desc;
  1870.      register struct file_entry *entry;
  1871. {
  1872.   register int len;
  1873.   struct exec *loc = (struct exec *) &entry->header;
  1874.  
  1875.   lseek (desc, entry->starting_offset, 0);
  1876.   len = read (desc, loc, sizeof (struct exec));
  1877.   if (len != sizeof (struct exec))
  1878.     fatal_with_file ("failure reading header of ", entry);
  1879. #if HOST_BYTE_ORDER != TARGET_BYTE_ORDER
  1880.   fix_exec_header_byte_order(loc);
  1881. #endif
  1882.   if (N_BADMAG (*loc))
  1883.     fatal_with_file ("bad magic number in ", entry);
  1884.  
  1885.   entry->header_read_flag = 1;
  1886. }
  1887.  
  1888. /* Read the symbols of file ENTRY into core.
  1889.    Assume it is already open, on descriptor DESC.
  1890.    Also read the length of the string table, which follows the symbol table,
  1891.    but don't read the contents of the string table.  */
  1892.  
  1893. void
  1894. read_entry_symbols (desc, entry)
  1895.      struct file_entry *entry;
  1896.      int desc;
  1897. {
  1898.   int str_size;
  1899.  
  1900.   if (!entry->header_read_flag)
  1901.     read_header (desc, entry);
  1902.  
  1903.   entry->symbols = (struct nlist *) xmalloc (entry->header.a_syms);
  1904.  
  1905.   lseek (desc, N_SYMOFF (entry->header) + entry->starting_offset, 0);
  1906.   if (entry->header.a_syms != read (desc, entry->symbols, entry->header.a_syms))
  1907.     fatal_with_file ("premature end of file in symbols of ", entry);
  1908. #if HOST_BYTE_ORDER != TARGET_BYTE_ORDER
  1909.   fix_symbol_byte_order(entry->symbols, entry->header.a_syms);
  1910. #endif
  1911.   lseek (desc, N_STROFF (entry->header) + entry->starting_offset, 0);
  1912.   if (sizeof str_size != read (desc, &str_size, sizeof str_size))
  1913.     fatal_with_file ("bad string table size in ", entry);
  1914. #if HOST_BYTE_ORDER != TARGET_BYTE_ORDER
  1915.   fix_byte_order(&str_size, sizeof(str_size));
  1916. #endif
  1917.   entry->string_size = str_size;
  1918. }
  1919.  
  1920. /* Read the string table of file ENTRY into core.
  1921.    Assume it is already open, on descriptor DESC.
  1922.    Also record whether a GDB symbol segment follows the string table.  */
  1923.  
  1924. void
  1925. read_entry_strings (desc, entry)
  1926.      struct file_entry *entry;
  1927.      int desc;
  1928. {
  1929.   int buffer;
  1930.  
  1931.   if (!entry->header_read_flag)
  1932.     read_header (desc, entry);
  1933.  
  1934.   lseek (desc, N_STROFF (entry->header) + entry->starting_offset, 0);
  1935.   if (entry->string_size != read (desc, entry->strings, entry->string_size))
  1936.     fatal_with_file ("premature end of file in strings of ", entry);
  1937.  
  1938.   /* While we are here, see if the file has a symbol segment at the end.
  1939.      For a separate file, just try reading some more.
  1940.      For a library member, compare current pos against total size.  */
  1941.   if (entry->superfile)
  1942.     {
  1943.       if (entry->total_size == N_STROFF (entry->header) + entry->string_size)
  1944.     return;
  1945.     }
  1946.   else
  1947.     {
  1948.       buffer = read (desc, &buffer, sizeof buffer);
  1949.       if (buffer == 0)
  1950.     return;
  1951.       if (buffer != sizeof buffer)
  1952.     fatal_with_file ("premature end of file in GDB symbol segment of ", entry);
  1953.     }
  1954.  
  1955.   entry->symseg_offset = N_STROFF (entry->header) + entry->string_size;
  1956. }
  1957.  
  1958. /* Read in the symbols of all input files.  */
  1959.  
  1960. void read_file_symbols (), read_entry_symbols (), read_entry_strings ();
  1961. void enter_file_symbols (), enter_global_ref (), search_library ();
  1962.  
  1963. void
  1964. load_symbols ()
  1965. {
  1966.   register int i;
  1967.  
  1968.   if (trace_files) fprintf (stderr, "Loading symbols:\n\n");
  1969.  
  1970.   for (i = 0; i < number_of_files; i++)
  1971.     {
  1972.       register struct file_entry *entry = &file_table[i];
  1973.       read_file_symbols (entry);
  1974.     }
  1975.  
  1976.   if (trace_files) fprintf (stderr, "\n");
  1977. }
  1978.  
  1979. /* If ENTRY is a rel file, read its symbol and string sections into core.
  1980.    If it is a library, search it and load the appropriate members
  1981.    (which means calling this function recursively on those members).  */
  1982.  
  1983. void
  1984. read_file_symbols (entry)
  1985.      register struct file_entry *entry;
  1986. {
  1987.   register int desc;
  1988.   register int len;
  1989.   struct exec hdr;
  1990.  
  1991.   desc = file_open (entry);
  1992.  
  1993.   len = read (desc, &hdr, sizeof hdr);
  1994.   if (len != sizeof hdr)
  1995.     fatal_with_file ("failure reading header of ", entry);
  1996. #if HOST_BYTE_ORDER != TARGET_BYTE_ORDER
  1997.   fix_exec_header_byte_order(&hdr);
  1998. #endif
  1999.   if (!N_BADMAG (hdr))
  2000.     {
  2001.       read_entry_symbols (desc, entry);
  2002.       entry->strings = (char *) alloca (entry->string_size);
  2003.       read_entry_strings (desc, entry);
  2004.       enter_file_symbols (entry);
  2005.       entry->strings = 0;
  2006.     }
  2007.   else
  2008.     {
  2009.       char armag[SARMAG];
  2010.  
  2011.       lseek (desc, 0, 0);
  2012.       if (SARMAG != read (desc, armag, SARMAG) || strncmp (armag, ARMAG, SARMAG))
  2013.     fatal_with_file ("malformed input file (not rel or archive) ", entry);
  2014.       entry->library_flag = 1;
  2015.       search_library (desc, entry);
  2016.     }
  2017.  
  2018.   file_close ();
  2019. }
  2020.  
  2021. /* Enter the external symbol defs and refs of ENTRY in the hash table.  */
  2022.  
  2023. void
  2024. enter_file_symbols (entry)
  2025.      struct file_entry *entry;
  2026. {
  2027.   register struct nlist
  2028.     *p,
  2029.     *end = entry->symbols + entry->header.a_syms / sizeof (struct nlist);
  2030.  
  2031.   if (trace_files) prline_file_name (entry, stderr);
  2032.  
  2033.   for (p = entry->symbols; p < end; p++)
  2034.     {
  2035.       if (p->n_type == (N_SETV | N_EXT)) continue;
  2036.       if (set_element_prefixes
  2037.       && set_element_prefixed_p (p->n_un.n_strx + entry->strings))
  2038.     p->n_type += (N_SETA - N_ABS);
  2039.       if (SET_ELEMENT_P (p->n_type))
  2040.     {
  2041.       set_symbol_count++;
  2042.       if (!relocatable_output)
  2043.         enter_global_ref (p, p->n_un.n_strx + entry->strings, entry);
  2044.     }
  2045.       else if (p->n_type == N_WARNING)
  2046.     {
  2047.       char *name = p->n_un.n_strx + entry->strings;
  2048.  
  2049.       /* Grab the next entry.  */
  2050.       p++;
  2051.       if (p->n_type != (N_UNDF | N_EXT))
  2052.         {
  2053.           fprintf (stderr, "%s: Warning symbol found in %s without external reference following.\n",
  2054.                progname, entry->filename);
  2055.           make_executable = 0;
  2056.           p--;        /* Process normally.  */
  2057.         }
  2058.       else
  2059.         {
  2060.           symbol *sp;
  2061.           char *sname = p->n_un.n_strx + entry->strings; 
  2062.           /* Deal with the warning symbol.  */
  2063.           enter_global_ref (p, p->n_un.n_strx + entry->strings, entry);
  2064.           sp = getsym (sname);
  2065.           sp->warning = (char *) xmalloc (strlen(name) + 1);
  2066.           strcpy (sp->warning, name);
  2067.           warning_count++;
  2068.         }
  2069.     }
  2070.       else if (p->n_type & N_EXT)
  2071.     enter_global_ref (p, p->n_un.n_strx + entry->strings, entry);
  2072.       else if (p->n_un.n_strx && !(p->n_type & (N_STAB | N_EXT)))
  2073.     {
  2074.       if ((p->n_un.n_strx + entry->strings)[0] != LPREFIX)
  2075.         non_L_local_sym_count++;
  2076.       local_sym_count++;
  2077.     }
  2078.       else debugger_sym_count++;
  2079.     }
  2080.  
  2081.    /* Count one for the local symbol that we generate,
  2082.       whose name is the file's name (usually) and whose address
  2083.       is the start of the file's text.  */
  2084.  
  2085.   local_sym_count++;
  2086.   non_L_local_sym_count++;
  2087. }
  2088.  
  2089. /* Enter one global symbol in the hash table.
  2090.    NLIST_P points to the `struct nlist' read from the file
  2091.    that describes the global symbol.  NAME is the symbol's name.
  2092.    ENTRY is the file entry for the file the symbol comes from.
  2093.  
  2094.    The `struct nlist' is modified by placing it on a chain of
  2095.    all such structs that refer to the same global symbol.
  2096.    This chain starts in the `refs' field of the symbol table entry
  2097.    and is chained through the `n_name'.  */
  2098. void
  2099. enter_global_ref (nlist_p, name, entry)
  2100.      register struct nlist *nlist_p;
  2101.      char *name;
  2102.      struct file_entry *entry;
  2103. {
  2104.   register symbol *sp = getsym (name);
  2105.   register int type = nlist_p->n_type;
  2106.   int oldref = sp->referenced;
  2107.   int olddef = sp->defined;
  2108.  
  2109.   nlist_p->n_un.n_name = (char *) sp->refs;
  2110.   sp->refs = nlist_p;
  2111.  
  2112.   sp->referenced = 1;
  2113.   if (type != (N_UNDF | N_EXT) || nlist_p->n_value)
  2114.     {
  2115.       if (!sp->defined || sp->defined == (N_UNDF | N_EXT))
  2116.     sp->defined = type;
  2117.  
  2118.       if (oldref && !olddef)
  2119.     /* It used to be undefined and we're defining it.  */
  2120.     undefined_global_sym_count--;
  2121.  
  2122.       if (!olddef && type == (N_UNDF | N_EXT) && nlist_p->n_value)
  2123.     {
  2124.       /* First definition and it's common.  */
  2125.       common_defined_global_count++;
  2126.       sp->max_common_size = nlist_p->n_value;
  2127.     }
  2128.       else if (olddef && sp->max_common_size && type != (N_UNDF | N_EXT))
  2129.     {
  2130.       /* It used to be common and we're defining it as
  2131.          something else.  */
  2132.       common_defined_global_count--;
  2133.       sp->max_common_size = 0;
  2134.     }
  2135.       else if (olddef && sp->max_common_size && type == (N_UNDF | N_EXT)
  2136.       && sp->max_common_size < nlist_p->n_value)
  2137.     /* It used to be common and this is a new common entry to
  2138.        which we need to pay attention.  */
  2139.     sp->max_common_size = nlist_p->n_value;
  2140.  
  2141.       /* Are we defining it as a set element?  */
  2142.       if (SET_ELEMENT_P (type)
  2143.       && (!olddef || (olddef && sp->max_common_size)))
  2144.     set_vector_count++;
  2145.       /* As an indirection?  */
  2146.       else if (type == (N_INDR | N_EXT))
  2147.     {
  2148.       /* Indirect symbols value should be modified to point
  2149.          a symbol being equivalenced to. */
  2150.       nlist_p->n_value =
  2151.         (unsigned int) getsym ((nlist_p + 1)->n_un.n_strx
  2152.                      + entry->strings);
  2153.       if ((symbol *) nlist_p->n_value == sp)
  2154.         {
  2155.           /* Somebody redefined a symbol to be itself.  */
  2156.           fprintf (stderr, "%s: Symbol %s indirected to itself.\n",
  2157.                entry->filename, name);
  2158.           /* Rewrite this symbol as being a global text symbol
  2159.          with value 0.  */
  2160.           nlist_p->n_type = sp->defined = N_TEXT | N_EXT;
  2161.           nlist_p->n_value = 0;
  2162.           /* Don't make the output executable.  */
  2163.           make_executable = 0;
  2164.         }
  2165.       else
  2166.         global_indirect_count++;
  2167.     }
  2168.     }
  2169.   else
  2170.     if (!oldref)
  2171. #ifndef DOLLAR_KLUDGE
  2172.       undefined_global_sym_count++;
  2173. #else
  2174.       {
  2175.     if (entry->superfile && type == (N_UNDF | N_EXT) && name[1] == '$')
  2176.       {
  2177.         /* This is an (ISI?) $-conditional; skip it */
  2178.         sp->referenced = 0;
  2179.         if (sp->trace)
  2180.           {
  2181.         fprintf (stderr, "symbol %s is a $-conditional ignored in ", sp->name);
  2182.         print_file_name (entry, stderr);
  2183.         fprintf (stderr, "\n");
  2184.           }
  2185.         return;
  2186.       }
  2187.     else
  2188.       undefined_global_sym_count++;
  2189.       }
  2190. #endif
  2191.  
  2192.   if (sp == end_symbol && entry->just_syms_flag && !T_flag_specified)
  2193.     text_start = nlist_p->n_value;
  2194.  
  2195.   if (sp->trace)
  2196.     {
  2197.       register char *reftype;
  2198.       switch (type & N_TYPE)
  2199.     {
  2200.     case N_UNDF:
  2201.       if (nlist_p->n_value)
  2202.         reftype = "defined as common";
  2203.       else reftype = "referenced";
  2204.       break;
  2205.  
  2206.     case N_ABS:
  2207.       reftype = "defined as absolute";
  2208.       break;
  2209.  
  2210.     case N_TEXT:
  2211.       reftype = "defined in text section";
  2212.       break;
  2213.  
  2214.     case N_DATA:
  2215.       reftype = "defined in data section";
  2216.       break;
  2217.  
  2218.     case N_BSS:
  2219.       reftype = "defined in BSS section";
  2220.       break;
  2221.  
  2222.     case N_SETT:
  2223.       reftype = "is a text set element";
  2224.       break;
  2225.  
  2226.     case N_SETD:
  2227.       reftype = "is a data set element";
  2228.       break;
  2229.  
  2230.     case N_SETB:
  2231.       reftype = "is a BSS set element";
  2232.       break;
  2233.  
  2234.     case N_SETA:
  2235.       reftype = "is an absolute set element";
  2236.       break;
  2237.  
  2238.     case N_SETV:
  2239.       reftype = "defined in data section as vector";
  2240.       break;
  2241.  
  2242.     case N_INDR:
  2243.       reftype = (char *) alloca (23
  2244.                      + strlen ((nlist_p + 1)->n_un.n_strx
  2245.                            + entry->strings));
  2246.       sprintf (reftype, "defined equivalent to %s",
  2247.            (nlist_p + 1)->n_un.n_strx + entry->strings);
  2248.       break;
  2249.  
  2250. #if TARGET_MACHINE==TARGET_SEQUENT
  2251.     case N_SHUNDF:
  2252.       reftype = "shared undf";
  2253.       break;
  2254.  
  2255. /* These conflict with cases above.
  2256.     case N_SHDATA:
  2257.       reftype = "shared data";
  2258.       break;
  2259.  
  2260.     case N_SHBSS:
  2261.       reftype = "shared BSS";
  2262.       break;
  2263. */
  2264.     default:
  2265.       reftype = "I don't know this type";
  2266.       break;
  2267. #endif
  2268.     }
  2269.  
  2270.       fprintf (stderr, "symbol %s %s in ", sp->name, reftype);
  2271.       print_file_name (entry, stderr);
  2272.       fprintf (stderr, "\n");
  2273.     }
  2274. }
  2275.  
  2276. /* This return 0 if the given file entry's symbol table does *not*
  2277.    contain the nlist point entry, and it returns the files entry
  2278.    pointer (cast to unsigned long) if it does. */
  2279.  
  2280. #if 0
  2281. unsigned long
  2282. contains_symbol (entry, n_ptr)
  2283.      struct file_entry *entry;
  2284.      register struct nlist *n_ptr;
  2285. {
  2286.   if (n_ptr >= entry->symbols &&
  2287.       n_ptr < (entry->symbols
  2288.            + (entry->header.a_syms / sizeof (struct nlist))))
  2289.     return (unsigned long) entry;
  2290.   return 0;
  2291. }
  2292. #endif
  2293.  
  2294.  
  2295. /* Searching libraries */
  2296.  
  2297. struct file_entry *decode_library_subfile ();
  2298. void linear_library (), symdef_library ();
  2299.  
  2300. /* Search the library ENTRY, already open on descriptor DESC.
  2301.    This means deciding which library members to load,
  2302.    making a chain of `struct file_entry' for those members,
  2303.    and entering their global symbols in the hash table.  */
  2304.  
  2305. void
  2306. search_library (desc, entry)
  2307.      int desc;
  2308.      struct file_entry *entry;
  2309. {
  2310.   int member_length;
  2311.   register char *name;
  2312.   register struct file_entry *subentry;
  2313.  
  2314.   if (!undefined_global_sym_count) return;
  2315.  
  2316.   /* Examine its first member, which starts SARMAG bytes in.  */
  2317.   subentry = decode_library_subfile (desc, entry, SARMAG, &member_length);
  2318.   if (!subentry) return;
  2319.  
  2320.   name = subentry->filename;
  2321.   free (subentry);
  2322.  
  2323.   /* Search via __.SYMDEF if that exists, else linearly.  */
  2324.  
  2325.   if (!strcmp (name, "__.SYMDEF"))
  2326.     symdef_library (desc, entry, member_length);
  2327.   else
  2328.     linear_library (desc, entry);
  2329. }
  2330.  
  2331. /* Construct and return a file_entry for a library member.
  2332.    The library's file_entry is library_entry, and the library is open on DESC.
  2333.    SUBFILE_OFFSET is the byte index in the library of this member's header.
  2334.    We store the length of the member into *LENGTH_LOC.  */
  2335.  
  2336. struct file_entry *
  2337. decode_library_subfile (desc, library_entry, subfile_offset, length_loc)
  2338.      int desc;
  2339.      struct file_entry *library_entry;
  2340.      int subfile_offset;
  2341.      int *length_loc;
  2342. {
  2343.   int bytes_read;
  2344.   register int namelen;
  2345.   int member_length;
  2346.   register char *name;
  2347.   struct ar_hdr hdr1;
  2348.   register struct file_entry *subentry;
  2349.  
  2350.   lseek (desc, subfile_offset, 0);
  2351.  
  2352.   bytes_read = read (desc, &hdr1, sizeof hdr1);
  2353.   if (!bytes_read)
  2354.     return 0;        /* end of archive */
  2355.  
  2356.   if (sizeof hdr1 != bytes_read)
  2357.     fatal_with_file ("malformed library archive ", library_entry);
  2358.  
  2359.   if (sscanf (hdr1.ar_size, "%d", &member_length) != 1)
  2360.     fatal_with_file ("malformatted header of archive member in ", library_entry);
  2361.  
  2362.   subentry = (struct file_entry *) xmalloc (sizeof (struct file_entry));
  2363.   bzero (subentry, sizeof (struct file_entry));
  2364.  
  2365.   for (namelen = 0;
  2366.        namelen < sizeof hdr1.ar_name
  2367.        && hdr1.ar_name[namelen] != 0 && hdr1.ar_name[namelen] != ' '
  2368.        && hdr1.ar_name[namelen] != '/';
  2369.        namelen++);
  2370.  
  2371.   name = (char *) xmalloc (namelen+1);
  2372.   strncpy (name, hdr1.ar_name, namelen);
  2373.   name[namelen] = 0;
  2374.  
  2375.   subentry->filename = name;
  2376.   subentry->local_sym_name = name;
  2377.   subentry->symbols = 0;
  2378.   subentry->strings = 0;
  2379.   subentry->subfiles = 0;
  2380.   subentry->starting_offset = subfile_offset + sizeof hdr1;
  2381.   subentry->superfile = library_entry;
  2382.   subentry->library_flag = 0;
  2383.   subentry->header_read_flag = 0;
  2384.   subentry->just_syms_flag = 0;
  2385.   subentry->chain = 0;
  2386.   subentry->total_size = member_length;
  2387.  
  2388.   (*length_loc) = member_length;
  2389.  
  2390.   return subentry;
  2391. }
  2392.  
  2393. int subfile_wanted_p ();
  2394.  
  2395. /* Search a library that has a __.SYMDEF member.
  2396.    DESC is a descriptor on which the library is open.
  2397.      The file pointer is assumed to point at the __.SYMDEF data.
  2398.    ENTRY is the library's file_entry.
  2399.    MEMBER_LENGTH is the length of the __.SYMDEF data.  */
  2400.  
  2401. void
  2402. symdef_library (desc, entry, member_length)
  2403.      int desc;
  2404.      struct file_entry *entry;
  2405.      int member_length;
  2406. {
  2407.   int *symdef_data = (int *) xmalloc (member_length);
  2408.   register struct symdef *symdef_base;
  2409.   char *sym_name_base;
  2410.   int number_of_symdefs;
  2411.   int length_of_strings;
  2412.   int not_finished;
  2413.   int bytes_read;
  2414.   register int i;
  2415.   struct file_entry *prev = 0;
  2416.   int prev_offset = 0;
  2417.  
  2418.   bytes_read = read (desc, symdef_data, member_length);
  2419.   if (bytes_read != member_length)
  2420.     fatal_with_file ("malformatted __.SYMDEF in ", entry);
  2421. #if HOST_BYTE_ORDER != TARGET_BYTE_ORDER
  2422.   fix_byte_order(symdef_data, sizeof(*symdef_data));
  2423. #endif
  2424.   number_of_symdefs = *symdef_data / sizeof (struct symdef);
  2425.   if (number_of_symdefs < 0 ||
  2426.        number_of_symdefs * sizeof (struct symdef) + 2 * sizeof (int) > member_length)
  2427.     fatal_with_file ("malformatted __.SYMDEF in ", entry);
  2428.  
  2429.   symdef_base = (struct symdef *) (symdef_data + 1);
  2430. #if HOST_BYTE_ORDER != TARGET_BYTE_ORDER
  2431.   for (i = 0; i < number_of_symdefs; ++i)
  2432.     {
  2433.       fix_byte_order(&symdef_base[i].symbol_name_string_index, sizeof(int));
  2434.       fix_byte_order(&symdef_base[i].library_member_offset, sizeof(int));
  2435.     }
  2436.   fix_byte_order(symdef_base + number_of_symdefs, sizeof(int));
  2437. #endif
  2438.   length_of_strings = *(int *) (symdef_base + number_of_symdefs);
  2439.  
  2440.   if (length_of_strings < 0
  2441.       || number_of_symdefs * sizeof (struct symdef) + length_of_strings
  2442.       + 2 * sizeof (int) != member_length)
  2443.     fatal_with_file ("malformatted __.SYMDEF in ", entry);
  2444.  
  2445.   sym_name_base = sizeof (int) + (char *) (symdef_base + number_of_symdefs);
  2446.  
  2447.   /* Check all the string indexes for validity.  */
  2448.  
  2449.   for (i = 0; i < number_of_symdefs; i++)
  2450.     {
  2451.       register int index = symdef_base[i].symbol_name_string_index;
  2452.       if (index < 0 || index >= length_of_strings
  2453.       || (index && *(sym_name_base + index - 1)))
  2454.     fatal_with_file ("malformatted __.SYMDEF in ", entry);
  2455.     }
  2456.  
  2457.   /* Search the symdef data for members to load.
  2458.      Do this until one whole pass finds nothing to load.  */
  2459.  
  2460.   not_finished = 1;
  2461.   while (not_finished)
  2462.     {
  2463.       not_finished = 0;
  2464.  
  2465.       /* Scan all the symbols mentioned in the symdef for ones that we need.
  2466.      Load the library members that contain such symbols.  */
  2467.  
  2468.       for (i = 0;
  2469.        (i < number_of_symdefs
  2470.         && (undefined_global_sym_count || common_defined_global_count));
  2471.        i++)
  2472.     if (symdef_base[i].symbol_name_string_index >= 0)
  2473.       {
  2474.         register symbol *sp;
  2475.  
  2476.         sp = getsym_soft (sym_name_base
  2477.                   + symdef_base[i].symbol_name_string_index);
  2478.  
  2479.         /* If we find a symbol that appears to be needed, think carefully
  2480.            about the archive member that the symbol is in.  */
  2481.  
  2482.         if (sp && ((sp->referenced && !sp->defined)
  2483. #if 1        
  2484.                || (sp->defined && sp->max_common_size))
  2485. #endif
  2486.                )
  2487.           {
  2488.         int junk;
  2489.         register int j;
  2490.         register int offset = symdef_base[i].library_member_offset;
  2491.         struct file_entry *subentry;
  2492.  
  2493.         /* Don't think carefully about any archive member
  2494.            more than once in a given pass.  */
  2495.  
  2496.         if (prev_offset == offset)
  2497.           continue;
  2498.         prev_offset = offset;
  2499.  
  2500.         /* Read the symbol table of the archive member.  */
  2501.  
  2502.         subentry = decode_library_subfile (desc, entry, offset, &junk);
  2503.         if (subentry == 0)
  2504.           fatal ("invalid offset for %s in symbol table of %s",
  2505.              sym_name_base
  2506.              + symdef_base[i].symbol_name_string_index,
  2507.              entry->filename);
  2508.         read_entry_symbols (desc, subentry);
  2509.         subentry->strings = (char *) malloc (subentry->string_size);
  2510.         read_entry_strings (desc, subentry);
  2511.  
  2512.         /* Now scan the symbol table and decide whether to load.  */
  2513.  
  2514.         if (!subfile_wanted_p (subentry))
  2515.           {
  2516.             free (subentry->symbols);
  2517.             free (subentry);
  2518.           }
  2519.         else
  2520.           {
  2521.             /* This member is needed; load it.
  2522.                Since we are loading something on this pass,
  2523.                we must make another pass through the symdef data.  */
  2524.  
  2525.             not_finished = 1;
  2526.  
  2527.             enter_file_symbols (subentry);
  2528.  
  2529.             if (prev)
  2530.               prev->chain = subentry;
  2531.             else entry->subfiles = subentry;
  2532.             prev = subentry;
  2533.  
  2534.             /* Clear out this member's symbols from the symdef data
  2535.                so that following passes won't waste time on them.  */
  2536.  
  2537.             for (j = 0; j < number_of_symdefs; j++)
  2538.               {
  2539.             if (symdef_base[j].library_member_offset == offset)
  2540.               symdef_base[j].symbol_name_string_index = -1;
  2541.               }
  2542.           }
  2543.  
  2544.         /* We'll read the strings again if we need them again.  */
  2545.         free (subentry->strings);
  2546.         subentry->strings = 0;
  2547.           }
  2548.       }
  2549.     }
  2550.  
  2551.   free (symdef_data);
  2552. }
  2553.  
  2554.  
  2555. /* Handle a subentry for a file with no __.SYMDEF. */
  2556.  
  2557. process_subentry (desc, subentry, entry, prev_addr)
  2558.      int desc;
  2559.      register struct file_entry *subentry;
  2560.      struct file_entry **prev_addr, *entry;
  2561. {
  2562.   register struct file_entry *prev = *prev_addr;
  2563.  
  2564.   read_entry_symbols (desc, subentry);
  2565.   subentry->strings = (char *) alloca (subentry->string_size);
  2566.   read_entry_strings (desc, subentry);
  2567.  
  2568.   if (!subfile_wanted_p (subentry))
  2569.     {
  2570.       free (subentry->symbols);
  2571.       free (subentry);
  2572.     }
  2573.   else
  2574.     {
  2575.       enter_file_symbols (subentry);
  2576.  
  2577.       if (prev)
  2578.     prev->chain = subentry;
  2579.       else
  2580.     entry->subfiles = subentry;
  2581.       prev = subentry;
  2582.       subentry->strings = 0; /* Since space will dissapear on return */
  2583.     }
  2584. }
  2585.  
  2586. /* Search a library that has no __.SYMDEF.
  2587.    ENTRY is the library's file_entry.
  2588.    DESC is the descriptor it is open on.  */
  2589.  
  2590. void
  2591. linear_library (desc, entry)
  2592.      int desc;
  2593.      struct file_entry *entry;
  2594. {
  2595.   struct file_entry *prev = 0;
  2596.   register int this_subfile_offset = SARMAG;
  2597.  
  2598.   while (undefined_global_sym_count || common_defined_global_count)
  2599.     {
  2600.       int member_length;
  2601.       register struct file_entry *subentry;
  2602.  
  2603.       subentry = decode_library_subfile (desc, entry, this_subfile_offset,
  2604.                      &member_length);
  2605.       if (!subentry) return;
  2606.  
  2607.       process_subentry (desc, subentry, entry, &prev);
  2608.       this_subfile_offset += member_length + sizeof (struct ar_hdr);
  2609.       if (this_subfile_offset & 1) this_subfile_offset++;
  2610.     }
  2611. }
  2612.  
  2613. /* ENTRY is an entry for a library member.
  2614.    Its symbols have been read into core, but not entered.
  2615.    Return nonzero if we ought to load this member.  */
  2616. int
  2617. subfile_wanted_p (entry)
  2618.      struct file_entry *entry;
  2619. {
  2620.   register struct nlist *p;
  2621.   register struct nlist *end
  2622.     = entry->symbols + entry->header.a_syms / sizeof (struct nlist);
  2623. #ifdef DOLLAR_KLUDGE
  2624.   register int dollar_cond = 0;
  2625. #endif
  2626.  
  2627.   for (p = entry->symbols; p < end; p++)
  2628.     {
  2629.       register int type = p->n_type;
  2630.       register char *name = p->n_un.n_strx + entry->strings;
  2631.  
  2632.       /* If the symbol has an interesting definition, we could
  2633.      potentially want it.  */
  2634.       if (type & N_EXT
  2635.       && (type != (N_UNDF | N_EXT) || p->n_value
  2636.  
  2637. #ifdef DOLLAR_KLUDGE
  2638.            || name[1] == '$'
  2639. #endif
  2640.           )
  2641.       && !SET_ELEMENT_P (type)
  2642.       && !set_element_prefixed_p (name))
  2643.     {
  2644.       register symbol *sp = getsym_soft (name);
  2645.  
  2646. #ifdef DOLLAR_KLUDGE
  2647.       if (name[1] == '$')
  2648.         {
  2649.           sp = getsym_soft (&name[2]);
  2650.           dollar_cond = 1;
  2651.           if (!sp) continue;
  2652.           if (sp->referenced)
  2653.         {
  2654.           if (write_map)
  2655.             {
  2656.               print_file_name (entry, stdout);
  2657.               fprintf (stdout, " needed due to $-conditional %s\n", name);
  2658.             }
  2659.           return 1;
  2660.         }
  2661.           continue;
  2662.         }
  2663. #endif
  2664.  
  2665.       /* If this symbol has not been hashed, we can't be looking for it. */
  2666.  
  2667.       if (!sp) continue;
  2668.  
  2669.       if ((sp->referenced && !sp->defined)
  2670.           || (sp->defined && sp->max_common_size))
  2671.         {
  2672.           /* This is a symbol we are looking for.  It is either
  2673.              not yet defined or defined as a common.  */
  2674. #ifdef DOLLAR_KLUDGE
  2675.           if (dollar_cond) continue;
  2676. #endif
  2677.           if (type == (N_UNDF | N_EXT))
  2678.         {
  2679.           /* Symbol being defined as common.
  2680.              Remember this, but don't load subfile just for this.  */
  2681.           /* If it didn't used to be common, up the count of
  2682.              common symbols.  */
  2683.           if (!sp->max_common_size)
  2684.             common_defined_global_count++;
  2685.           if (sp->max_common_size < p->n_value)
  2686.             sp->max_common_size = p->n_value;
  2687.           if (!sp->defined)
  2688.             undefined_global_sym_count--;
  2689.           sp->defined = 1;
  2690.           continue;
  2691.         }
  2692.  
  2693. #if TARGET==TARGET_SEQUENT
  2694.         /* Don't resolve common (data) symbols with text symbols*/
  2695.           if (type == (N_EXT | N_TEXT)) {
  2696.         int dontresolve = 0;
  2697.         struct nlist *p, *next;
  2698.  
  2699.         for (p = sp->refs; p; p = next) {
  2700.           if (p->n_value && (p->n_type == (N_UNDF | N_EXT))) {
  2701.               if (trace_files)
  2702.                   fprintf(stdout,
  2703.                       "refs check fail: symbol %s, ref type= 0x%x, ref value= 0x%x, sym type = 0x%x\n",
  2704.                       sp->name, p->n_type, p->n_value, type);
  2705.             dontresolve = 1;
  2706.             break;
  2707.           }
  2708.           next = (struct nlist *) p->n_un.n_name;
  2709.         }
  2710.         if (sp->trace) {
  2711.           fprintf(stdout, 
  2712.             "symbol %s can't use text sym def in file ", sp->name);
  2713.           print_file_name(entry, stdout);
  2714.           fprintf(stdout, "\n");
  2715.         }
  2716.         if (dontresolve)
  2717.           continue;
  2718.           }
  2719. #endif        
  2720.           if (write_map)
  2721.         {
  2722.           print_file_name (entry, stdout);
  2723.           fprintf (stdout, " needed due to %s\n", sp->name);
  2724.         }
  2725.           return 1;
  2726.         }
  2727.     }
  2728.     }
  2729.  
  2730.   return 0;
  2731. }
  2732.  
  2733. void consider_file_section_lengths (), relocate_file_addresses ();
  2734.  
  2735. /* Having entered all the global symbols and found the sizes of sections
  2736.    of all files to be linked, make all appropriate deductions from this data.
  2737.  
  2738.    We propagate global symbol values from definitions to references.
  2739.    We compute the layout of the output file and where each input file's
  2740.    contents fit into it.  */
  2741.  
  2742. void
  2743. digest_symbols ()
  2744. {
  2745.   register int i;
  2746.   int setv_fill_count = 0;
  2747.  
  2748.   if (trace_files)
  2749.     fprintf (stderr, "Digesting symbol information:\n\n");
  2750.  
  2751.   /* Compute total size of sections */
  2752.  
  2753.   each_file (consider_file_section_lengths, 0);
  2754.  
  2755.   /* If necessary, pad text section to full page in the file.
  2756.      Include the padding in the text segment size.  */
  2757.  
  2758. #ifdef NMAGIC
  2759.   if (magic == ZMAGIC || magic == NMAGIC)
  2760. #else /* NMAGIC */
  2761. #ifdef SMAGIC
  2762.   if (magic == ZMAGIC || magic == SMAGIC)
  2763. #else /* SMAGIC */
  2764.   if (magic == ZMAGIC)
  2765. #endif /* SMAGIC */
  2766. #endif /* NMAGIC */
  2767.     {
  2768.       int text_end = text_size + N_TXTOFF (outheader);
  2769.       text_pad = ((text_end + page_size - 1) & (- page_size)) - text_end;
  2770.       text_size += text_pad;
  2771.     }
  2772.  
  2773.   outheader.a_text = text_size;
  2774. #if TARGET_MACHINE==TARGET_SEQUENT
  2775.   outheader.a_text += N_ADDRADJ (outheader);
  2776. #endif
  2777.  
  2778.   /* Make the data segment address start in memory on a suitable boundary.  */
  2779.  
  2780.   if (! Tdata_flag_specified)
  2781.     data_start = N_DATADDR (outheader) + text_start - N_TXTADDR (outheader);
  2782. #ifdef foobar
  2783.   /* Make sure bss starts out aligned as much as anyone can want.  */
  2784.  
  2785.   data_size = (data_size + sizeof(double) - 1) & ~(sizeof(double)-1);
  2786. #endif
  2787.  
  2788.   if (Z_flag_specified)
  2789.       data_start += data_gap;
  2790.  
  2791.   /* Set up the set element vector */
  2792.  
  2793.   if (!relocatable_output)
  2794.     {
  2795.       /* The set sector size is the number of set elements + a word
  2796.          for each symbol for the length word at the beginning of the
  2797.      vector, plus a word for each symbol for a zero at the end of
  2798.      the vector (for incremental linking).  */
  2799.       set_sect_size =
  2800.         (2 * set_symbol_count + set_vector_count) * sizeof (unsigned long);
  2801.       set_sect_start = data_start + data_size;
  2802.       data_size += set_sect_size;
  2803.       set_vectors = (unsigned long *) xmalloc (set_sect_size);
  2804. #if 1
  2805.       setv_fill_count = 0;
  2806. #endif      
  2807.     }
  2808.  
  2809.   /* Compute start addresses of each file's sections and symbols.  */
  2810.  
  2811.   each_full_file (relocate_file_addresses, 0);
  2812.  
  2813.   /* Now, for each symbol, verify that it is defined globally at most once.
  2814.      Put the global value into the symbol entry.
  2815.      Common symbols are allocated here, in the BSS section.
  2816.      Each defined symbol is given a '->defined' field
  2817.       which is the correct N_ code for its definition,
  2818.       except in the case of common symbols with -r.
  2819.      Then make all the references point at the symbol entry
  2820.      instead of being chained together. */
  2821.  
  2822.   defined_global_sym_count = 0;
  2823.  
  2824.   for (i = 0; i < TABSIZE; i++)
  2825.     {
  2826.       register symbol *sp;
  2827.       for (sp = symtab[i]; sp; sp = sp->link)
  2828.     {
  2829.       /* For each symbol */
  2830.       register struct nlist *p, *next;
  2831.       int defs = 0, com = sp->max_common_size;
  2832.       struct nlist *first_definition;
  2833.       for (p = sp->refs; p; p = next)
  2834.         {
  2835.           register int type = p->n_type;
  2836.  
  2837.           if (SET_ELEMENT_P (type))
  2838.         {
  2839.           if (relocatable_output)
  2840.             fatal ("internal: global ref to set element with -r");
  2841.           if (!defs++)
  2842.             {
  2843.               sp->value = set_sect_start
  2844.             + setv_fill_count++ * sizeof (unsigned long);
  2845.               sp->defined = N_SETV | N_EXT;
  2846.               first_definition = p;
  2847.             }
  2848.           else if ((sp->defined & ~N_EXT) != N_SETV)
  2849.             {
  2850.               sp->multiply_defined = 1;
  2851.               multiple_def_count++;
  2852.             }
  2853.           set_vectors[setv_fill_count++] = p->n_value;
  2854.         }
  2855.           else if ((type & N_EXT) && type != (N_UNDF | N_EXT))
  2856.         {
  2857.           /* non-common definition */
  2858.           if (defs++ && sp->value != p->n_value)
  2859.             {
  2860.               sp->multiply_defined = 1;
  2861.               multiple_def_count++;
  2862.             }
  2863.           sp->value = p->n_value;
  2864.           sp->defined = type;
  2865.           first_definition = p;
  2866.         }
  2867.           next = (struct nlist *) p->n_un.n_name;
  2868.           p->n_un.n_name = (char *) sp;
  2869.         }
  2870.       /* Allocate as common if defined as common and not defined for real */
  2871.       if (com && !defs)
  2872.         {
  2873.           if (!relocatable_output || force_common_definition)
  2874.         {
  2875.           int align = sizeof (int);
  2876.  
  2877.           /* Round up to nearest sizeof (int).  I don't know
  2878.              whether this is necessary or not (given that
  2879.              alignment is taken care of later), but it's
  2880.              traditional, so I'll leave it in.  Note that if
  2881.              this size alignment is ever removed, ALIGN above
  2882.              will have to be initialized to 1 instead of
  2883.              sizeof (int).  */
  2884.           
  2885.           com = (com + sizeof (int) - 1) & (- sizeof (int));
  2886.  
  2887.           while (!(com & align))
  2888.             align <<= 1;
  2889.  
  2890.           align = align > MAX_ALIGNMENT ? MAX_ALIGNMENT : align;
  2891.  
  2892.           bss_size = ((((bss_size + data_size + data_start)
  2893.                   + (align - 1)) & (- align))
  2894.                   - data_size - data_start);
  2895.  
  2896.           sp->value = data_start + data_size + bss_size;
  2897.           sp->defined = N_BSS | N_EXT;
  2898.           bss_size += com;
  2899.           if (write_map)
  2900.             printf ("Allocating common %s: %x at %x\n",
  2901.                 sp->name, com, sp->value);
  2902.         }
  2903.           else
  2904.         {
  2905.           sp->defined = 0;
  2906.           undefined_global_sym_count++;
  2907.         }
  2908.         }
  2909.       /* Set length word at front of vector and zero byte at end.
  2910.          Reverse the vector itself to put it in file order.  */
  2911.       if ((sp->defined & ~N_EXT) == N_SETV)
  2912.         {
  2913.           unsigned long length_word_index =
  2914.             (sp->value - set_sect_start) / sizeof (unsigned long);
  2915.           unsigned long i, tmp;
  2916.  
  2917.           set_vectors[length_word_index] =
  2918.             setv_fill_count - 1 - length_word_index;
  2919.           /* Reverse the vector.  */
  2920.           for (i = 1;
  2921.            i < (setv_fill_count - length_word_index - 1) / 2 + 1;
  2922.            i++)
  2923.         {
  2924.           tmp = set_vectors[length_word_index + i];
  2925.           set_vectors[length_word_index + i] =
  2926.             set_vectors[setv_fill_count - i];
  2927.           set_vectors[setv_fill_count - i] = tmp;
  2928.         }
  2929.  
  2930.           set_vectors[setv_fill_count++] = 0;
  2931.         }
  2932.       if (sp->defined)
  2933.         defined_global_sym_count++;
  2934.     }
  2935.     }
  2936.  
  2937. #ifdef foobar
  2938.   /* Make sure end of bss is aligned as much as anyone can want.  */
  2939.  
  2940.   bss_size = (bss_size + sizeof(double) - 1) & ~(sizeof(double)-1);
  2941. #endif
  2942.  
  2943.   if (end_symbol)        /* These are null if -r.  */
  2944.     {
  2945.       etext_symbol->value = text_size + text_start;
  2946.       edata_symbol->value = data_start + data_size;
  2947.       end_symbol->value = data_start + data_size + bss_size;
  2948.     }
  2949.  
  2950.   /* Figure the data_pad now, so that it overlaps with the bss addresses.  */
  2951.  
  2952.   if (specified_data_size && specified_data_size > data_size)
  2953.     data_pad = specified_data_size - data_size;
  2954.  
  2955. #ifdef SMAGIC
  2956.   if (magic == SMAGIC || magic == ZMAGIC)
  2957. #else
  2958.   if (magic == ZMAGIC)
  2959. #endif
  2960.     data_pad = ((data_pad + data_size + page_size - 1) & (- page_size))
  2961.     - data_size;
  2962.  
  2963.   bss_size -= data_pad;
  2964.   if (bss_size < 0) bss_size = 0;
  2965.  
  2966.   data_size += data_pad;
  2967. }
  2968.  
  2969. /* Accumulate the section sizes of input file ENTRY
  2970.    into the section sizes of the output file.  */
  2971.  
  2972. void
  2973. consider_file_section_lengths (entry)
  2974.      register struct file_entry *entry;
  2975. {
  2976.   if (entry->just_syms_flag)
  2977.     return;
  2978.  
  2979.   entry->text_start_address = text_size;
  2980.   /* If there were any vectors, we need to chop them off */
  2981.   text_size += entry->header.a_text;
  2982.   entry->data_start_address = data_size;
  2983.   data_size += entry->header.a_data;
  2984.   entry->bss_start_address = bss_size;
  2985.   bss_size += entry->header.a_bss;
  2986.  
  2987.   text_reloc_size += entry->header.a_trsize;
  2988.   data_reloc_size += entry->header.a_drsize;
  2989. }
  2990.  
  2991. /* Determine where the sections of ENTRY go into the output file,
  2992.    whose total section sizes are already known.
  2993.    Also relocate the addresses of the file's local and debugger symbols.  */
  2994.  
  2995. void
  2996. relocate_file_addresses (entry)
  2997.      register struct file_entry *entry;
  2998. {
  2999.   entry->text_start_address += text_start;
  3000.   /* Note that `data_start' and `data_size' have not yet been
  3001.      adjusted for `data_pad'.  If they had been, we would get the wrong
  3002.      results here.  */
  3003.   entry->data_start_address += data_start;
  3004.   entry->bss_start_address += data_start + data_size;
  3005.  
  3006.   {
  3007.     register struct nlist *p;
  3008.     register struct nlist *end
  3009.       = entry->symbols + entry->header.a_syms / sizeof (struct nlist);
  3010.  
  3011.     for (p = entry->symbols; p < end; p++)
  3012.       {
  3013.     /* If this belongs to a section, update it by the section's start address */
  3014.     register int type = p->n_type & N_TYPE;
  3015.  
  3016.     switch (type)
  3017.       {
  3018.       case N_TEXT:
  3019.       case N_SETT:
  3020.         p->n_value += entry->text_start_address;
  3021.         break;
  3022.       case N_DATA:
  3023.       case N_SETV:
  3024.       case N_SETD:
  3025.         /* A symbol whose value is in the data section
  3026.            is present in the input file as if the data section
  3027.            started at an address equal to the length of the file's text.  */
  3028.         p->n_value += entry->data_start_address - entry->header.a_text;
  3029.         break;
  3030.       case N_BSS:
  3031.       case N_SETB:
  3032.         /* likewise for symbols with value in BSS.  */
  3033.         p->n_value += entry->bss_start_address
  3034.           - entry->header.a_text - entry->header.a_data;
  3035.         break;
  3036.       }
  3037.       }
  3038.   }
  3039. }
  3040.  
  3041. void describe_file_sections (), list_file_locals ();
  3042.  
  3043. /* Print a complete or partial map of the output file.  */
  3044.  
  3045. void
  3046. print_symbols (outfile)
  3047.      FILE *outfile;
  3048. {
  3049.   register int i;
  3050.  
  3051.   fprintf (outfile, "\nFiles:\n\n");
  3052.  
  3053.   each_file (describe_file_sections, outfile);
  3054.  
  3055.   fprintf (outfile, "\nGlobal symbols:\n\n");
  3056.  
  3057.   for (i = 0; i < TABSIZE; i++)
  3058.     {
  3059.       register symbol *sp;
  3060.       for (sp = symtab[i]; sp; sp = sp->link)
  3061.     {
  3062.       if (sp->defined == 1)
  3063.         fprintf (outfile, "  %s: common, length 0x%x\n", sp->name, sp->max_common_size);
  3064.       if (sp->defined)
  3065.         fprintf (outfile, "  %s: 0x%x\n", sp->name, sp->value);
  3066.       else if (sp->referenced)
  3067.         fprintf (outfile, "  %s: undefined\n", sp->name);
  3068.     }
  3069.     }
  3070.  
  3071.   each_file (list_file_locals, outfile);
  3072. }
  3073.  
  3074. void
  3075. describe_file_sections (entry, outfile)
  3076.      struct file_entry *entry;
  3077.      FILE *outfile;
  3078. {
  3079.   fprintf (outfile, "  ");
  3080.   print_file_name (entry, outfile);
  3081.   if (entry->just_syms_flag)
  3082.     fprintf (outfile, " symbols only\n", 0);
  3083.   else
  3084.     fprintf (outfile, " text %x(%x), data %x(%x), bss %x(%x) hex\n",
  3085.          entry->text_start_address, entry->header.a_text,
  3086.          entry->data_start_address, entry->header.a_data,
  3087.          entry->bss_start_address, entry->header.a_bss);
  3088. }
  3089.  
  3090. void
  3091. list_file_locals (entry, outfile)
  3092.      struct file_entry *entry;
  3093.      FILE *outfile;
  3094. {
  3095.   register struct nlist
  3096.     *p,
  3097.     *end = entry->symbols + entry->header.a_syms / sizeof (struct nlist);
  3098.  
  3099.   entry->strings = (char *) alloca (entry->string_size);
  3100.   read_entry_strings (file_open (entry), entry);
  3101.  
  3102.   fprintf (outfile, "\nLocal symbols of ");
  3103.   print_file_name (entry, outfile);
  3104.   fprintf (outfile, ":\n\n");
  3105.  
  3106.   for (p = entry->symbols; p < end; p++)
  3107.     /* If this is a definition,
  3108.        update it if necessary by this file's start address.  */
  3109.     if (!(p->n_type & (N_STAB | N_EXT)))
  3110.       fprintf (outfile, "  %s: 0x%x\n",
  3111.            entry->strings + p->n_un.n_strx, p->n_value);
  3112.  
  3113.   entry->strings = 0;        /* All done with them.  */
  3114. }
  3115.  
  3116.  
  3117. /* Static vars for do_warnings and subroutines of it */
  3118. int list_unresolved_refs;    /* List unresolved refs */
  3119. int list_warning_symbols;    /* List warning syms */
  3120. int list_multiple_defs;        /* List multiple definitions */
  3121.  
  3122. /*
  3123.  * Structure for communication between do_file_warnings and it's
  3124.  * helper routines.  Will in practice be an array of three of these:
  3125.  * 0) Current line, 1) Next line, 2) Source file info.
  3126.  */
  3127. struct line_debug_entry
  3128. {
  3129.   int line;
  3130.   char *filename;
  3131.   struct nlist *Sym;
  3132. };
  3133.  
  3134. void qsort ();
  3135. /*
  3136.  * Helper routines for do_file_warnings.
  3137.  */
  3138.  
  3139. /* Return an integer less than, equal to, or greater than 0 as per the
  3140.    relation between the two relocation entries.  Used by qsort.  */
  3141.  
  3142. int
  3143. relocation_entries_relation (rel1, rel2)
  3144.      struct relocation_info *rel1, *rel2;
  3145. {
  3146.   return RELOC_ADDRESS(rel1) - RELOC_ADDRESS(rel2);
  3147. }
  3148.  
  3149. /* Moves to the next debugging symbol in the file.  USE_DATA_SYMBOLS
  3150.    determines the type of the debugging symbol to look for (DSLINE or
  3151.    SLINE).  STATE_POINTER keeps track of the old and new locatiosn in
  3152.    the file.  It assumes that state_pointer[1] is valid; ie
  3153.    that it.sym points into some entry in the symbol table.  If
  3154.    state_pointer[1].sym == 0, this routine should not be called.  */
  3155.  
  3156. int
  3157. next_debug_entry (use_data_symbols, state_pointer)
  3158.      register int use_data_symbols;
  3159.      /* Next must be passed by reference! */
  3160.      struct line_debug_entry state_pointer[3];
  3161. {
  3162.   register struct line_debug_entry
  3163.     *current = state_pointer,
  3164.     *next = state_pointer + 1,
  3165.     /* Used to store source file */
  3166.     *source = state_pointer + 2;
  3167.   struct file_entry *entry = (struct file_entry *) source->Sym;
  3168.  
  3169.   current->Sym = next->Sym;
  3170.   current->line = next->line;
  3171.   current->filename = next->filename;
  3172.  
  3173.   while (++(next->Sym) < (entry->symbols
  3174.               + entry->header.a_syms/sizeof (struct nlist)))
  3175.     {
  3176.       /* n_type is a char, and N_SOL, N_EINCL and N_BINCL are > 0x80, so
  3177.        * may look negative...therefore, must mask to low bits
  3178.        */
  3179.       switch (next->Sym->n_type & 0xff) 
  3180.     {
  3181.     case N_SLINE:
  3182.       if (use_data_symbols) continue;
  3183.       next->line = next->Sym->n_desc;
  3184.       return 1;
  3185.     case N_DSLINE:
  3186.       if (!use_data_symbols) continue;
  3187.       next->line = next->Sym->n_desc;
  3188.       return 1;
  3189. #ifdef HAVE_SUN_STABS
  3190.     case N_EINCL:
  3191.       next->filename = source->filename;
  3192.       continue;
  3193. #endif
  3194.     case N_SO:
  3195.       source->filename = next->Sym->n_un.n_strx + entry->strings;
  3196.       source->line++;
  3197. #ifdef HAVE_SUN_STABS
  3198.     case N_BINCL:
  3199. #endif
  3200.     case N_SOL:
  3201.       next->filename =
  3202.         next->Sym->n_un.n_strx + entry->strings;
  3203.     default:
  3204.       continue;
  3205.     }
  3206.     }
  3207.   next->Sym = (struct nlist *) 0;
  3208.   return 0;
  3209. }
  3210.  
  3211. /* Create a structure to save the state of a scan through the debug
  3212.    symbols.  USE_DATA_SYMBOLS is set if we should be scanning for
  3213.    DSLINE's instead of SLINE's.  entry is the file entry which points
  3214.    at the symbols to use.  */
  3215.  
  3216. struct line_debug_entry *
  3217. init_debug_scan (use_data_symbols, entry)
  3218.      int use_data_symbols;
  3219.      struct file_entry *entry;
  3220. {
  3221.   struct line_debug_entry
  3222.     *state_pointer =
  3223.         (struct line_debug_entry *)
  3224.         xmalloc (3 * sizeof (struct line_debug_entry));
  3225.   register struct line_debug_entry
  3226.     *current = state_pointer,
  3227.     *next = state_pointer + 1,
  3228.     *source = state_pointer + 2; /* Used to store source file */
  3229.  
  3230.   struct nlist *tmp;
  3231.  
  3232.   for (tmp = entry->symbols;
  3233.        tmp < (entry->symbols
  3234.           + entry->header.a_syms/sizeof (struct nlist));
  3235.        tmp++)
  3236.     if (tmp->n_type == (int) N_SO)
  3237.       break;
  3238.  
  3239.   if (tmp >= (entry->symbols
  3240.           + entry->header.a_syms/sizeof (struct nlist)))
  3241.     {
  3242.       /* I believe this translates to "We lose" */
  3243.       current->filename = next->filename = entry->filename;
  3244.       current->line = next->line = -1;
  3245.       current->Sym = next->Sym = (struct nlist *) 0;
  3246.       return state_pointer;
  3247.     }
  3248.  
  3249.   next->line = source->line = 0;
  3250.   next->filename = source->filename
  3251.     = (tmp->n_un.n_strx + entry->strings);
  3252.   source->Sym = (struct nlist *) entry;
  3253.   next->Sym = tmp;
  3254.  
  3255.   next_debug_entry (use_data_symbols, state_pointer); /* To setup next */
  3256.  
  3257.   if (!next->Sym)        /* No line numbers for this section; */
  3258.                 /* setup output results as appropriate */
  3259.     {
  3260.       if (source->line)
  3261.     {
  3262.       current->filename = source->filename = entry->filename;
  3263.       current->line = -1;    /* Don't print lineno */
  3264.     }
  3265.       else
  3266.     {
  3267.       current->filename = source->filename;
  3268.       current->line = 0;
  3269.     }
  3270.       return state_pointer;
  3271.     }
  3272.  
  3273.  
  3274.   next_debug_entry (use_data_symbols, state_pointer); /* To setup current */
  3275.  
  3276.   return state_pointer;
  3277. }
  3278.  
  3279. /* Takes an ADDRESS (in either text or data space) and a STATE_POINTER
  3280.    which describes the current location in the implied scan through
  3281.    the debug symbols within the file which ADDRESS is within, and
  3282.    returns the source line number which corresponds to ADDRESS.  */
  3283.   
  3284. int
  3285. address_to_line (address, state_pointer)
  3286.      unsigned long address;
  3287.      /* Next must be passed by reference! */
  3288.      struct line_debug_entry state_pointer[3];
  3289. {
  3290.   struct line_debug_entry
  3291.     *current = state_pointer,
  3292.     *next = state_pointer + 1;
  3293.   struct line_debug_entry *tmp_pointer;
  3294.  
  3295.   int use_data_symbols;
  3296.  
  3297.   if (next->Sym)
  3298.     use_data_symbols = (next->Sym->n_type & N_TYPE) == N_DATA;
  3299.   else
  3300.     return current->line;
  3301.  
  3302.   /* Go back to the beginning if we've already passed it.  */
  3303.   if (current->Sym->n_value > address)
  3304.     {
  3305.       tmp_pointer = init_debug_scan (use_data_symbols,
  3306.                      (struct file_entry *)
  3307.                      ((state_pointer + 2)->Sym));
  3308.       state_pointer[0] = tmp_pointer[0];
  3309.       state_pointer[1] = tmp_pointer[1];
  3310.       state_pointer[2] = tmp_pointer[2];
  3311.       free (tmp_pointer);
  3312.     }
  3313.  
  3314.   /* If we're still in a bad way, return -1, meaning invalid line.  */
  3315.   if (current->Sym->n_value > address)
  3316.     return -1;
  3317.  
  3318.   while (next->Sym
  3319.      && next->Sym->n_value <= address
  3320.      && next_debug_entry (use_data_symbols, state_pointer))
  3321.     ;
  3322.   return current->line;
  3323. }
  3324.  
  3325.  
  3326. /* Macros for manipulating bitvectors.  */
  3327. #define    BIT_SET_P(bv, index)    ((bv)[(index) >> 3] & 1 << ((index) & 0x7))
  3328. #define    SET_BIT(bv, index)    ((bv)[(index) >> 3] |= 1 << ((index) & 0x7))
  3329.  
  3330. /* This routine will scan through the relocation data of file ENTRY,
  3331.    printing out references to undefined symbols and references to
  3332.    symbols defined in files with N_WARNING symbols.  If DATA_SEGMENT
  3333.    is non-zero, it will scan the data relocation segment (and use
  3334.    N_DSLINE symbols to track line number); otherwise it will scan the
  3335.    text relocation segment.  Warnings will be printed on the output
  3336.    stream OUTFILE.  Eventually, every nlist symbol mapped through will
  3337.    be marked in the NLIST_BITVECTOR, so we don't repeat ourselves when
  3338.    we scan the nlists themselves.  */
  3339.  
  3340. do_relocation_warnings (entry, data_segment, outfile, nlist_bitvector)
  3341.      struct file_entry *entry;
  3342.      int data_segment;
  3343.      FILE *outfile;
  3344.      unsigned char *nlist_bitvector;
  3345. {
  3346.   struct relocation_info
  3347.     *reloc_start = data_segment ? entry->datarel : entry->textrel,
  3348.     *reloc;
  3349.   int reloc_size =
  3350.     (data_segment ? entry->header.a_drsize : entry->header.a_trsize)
  3351.        / sizeof (struct relocation_info);
  3352.   int start_of_segment =
  3353.     (data_segment ? entry->data_start_address : entry->text_start_address);
  3354.   struct nlist *start_of_syms = entry->symbols;
  3355.   struct line_debug_entry *state_pointer =
  3356.     init_debug_scan (data_segment != 0, entry);
  3357.   register struct line_debug_entry
  3358.     *current = state_pointer;
  3359.   /* Assigned to generally static values; should not be written into.  */
  3360.   char *errfmt;
  3361.   /* Assigned to alloca'd values cand copied into; should be freed
  3362.      when done.  */
  3363.   char *errmsg;
  3364.   int invalidate_line_number;
  3365.  
  3366.   /* We need to sort the relocation info here.  Sheesh, so much effort
  3367.      for one lousy error optimization. */
  3368.  
  3369.   qsort (reloc_start, reloc_size, sizeof (struct relocation_info),
  3370.      relocation_entries_relation);
  3371.  
  3372.   for (reloc = reloc_start;
  3373.        reloc < (reloc_start + reloc_size);
  3374.        reloc++)
  3375.     {
  3376.       register struct nlist *s;
  3377.       register symbol *g;
  3378.  
  3379.       /* If the relocation isn't resolved through a symbol, continue */
  3380.       if (!RELOC_EXTERN_P(reloc))
  3381.     continue;
  3382.  
  3383.       s = &(entry->symbols[RELOC_SYMBOL(reloc)]);
  3384.  
  3385.       /* Local symbols shouldn't ever be used by relocation info, so
  3386.      the next should be safe.
  3387.      This is, of course, wrong.  References to local BSS symbols can be
  3388.      the targets of relocation info, and they can (must) be
  3389.      resolved through symbols.  However, these must be defined properly,
  3390.      (the assembler would have caught it otherwise), so we can
  3391.      ignore these cases.  */
  3392.       if (!(s->n_type & N_EXT))
  3393.     continue;
  3394.  
  3395.       g = (symbol *) s->n_un.n_name;
  3396.       errmsg = 0;
  3397.  
  3398.       if (!g->defined && list_unresolved_refs) /* Reference */
  3399.     {
  3400.       /* Mark as being noted by relocation warning pass.  */
  3401.       SET_BIT (nlist_bitvector, s - start_of_syms);
  3402.       
  3403.       if (g->undef_refs >= MAX_UREFS_PRINTED)    /* Listed too many */
  3404.         continue;
  3405.  
  3406.       /* Undefined symbol which we should mention */
  3407.  
  3408.       if (++(g->undef_refs) == MAX_UREFS_PRINTED)
  3409.         {
  3410.           errfmt = "More undefined symbol %s refs follow";
  3411.           invalidate_line_number = 1;
  3412.         }
  3413.       else
  3414.         {
  3415.           errfmt = "Undefined symbol %s referenced from %s segment";
  3416.           invalidate_line_number = 0;
  3417.         }
  3418.     }
  3419.       else                         /* Defined */
  3420.     {
  3421.       /* Potential symbol warning here */
  3422.       if (!g->warning) continue;
  3423.  
  3424.       /* Mark as being noted by relocation warning pass.  */
  3425.       SET_BIT (nlist_bitvector, s - start_of_syms);
  3426.       
  3427.       errfmt = 0;
  3428.       errmsg = g->warning;
  3429.       invalidate_line_number = 0;
  3430.     }
  3431.       
  3432.  
  3433.       /* If errfmt == 0, errmsg has already been defined.  */
  3434.       if (errfmt != 0)
  3435.     {
  3436.       char *nm;
  3437.  
  3438.       if (demangler == NULL || (nm = (*demangler)(g->name)) == NULL)
  3439.         nm = g->name;
  3440.       errmsg = (char *) xmalloc (strlen (errfmt) + strlen (nm) + 1);
  3441.       sprintf (errmsg, errfmt, nm, data_segment ? "data" : "text");
  3442.       if (nm != g->name)
  3443.         free (nm);
  3444.     }
  3445.  
  3446.       address_to_line (RELOC_ADDRESS (reloc) + start_of_segment,
  3447.                state_pointer);
  3448.  
  3449.       if (current->line >=0)
  3450.     fprintf (outfile, "%s:%d: %s\n", current->filename,
  3451.          invalidate_line_number ? 0 : current->line, errmsg);
  3452.       else
  3453.     fprintf (outfile, "%s: %s\n", current->filename, errmsg);
  3454.  
  3455.       if (errfmt != 0)
  3456.     free (errmsg);
  3457.     }
  3458.  
  3459.   free (state_pointer);
  3460. }
  3461.  
  3462. /* Print on OUTFILE a list of all warnings generated by references
  3463.    and/or definitions in the file ENTRY.  List source file and line
  3464.    number if possible, just the .o file if not. */
  3465.  
  3466. void
  3467. do_file_warnings (entry, outfile)
  3468.      struct file_entry *entry;
  3469.      FILE *outfile;
  3470. {
  3471.   int number_of_syms = entry->header.a_syms / sizeof (struct nlist);
  3472.   unsigned char *nlist_bitvector =
  3473.     (unsigned char *) alloca ((number_of_syms >> 3) + 1);
  3474.   struct line_debug_entry *text_scan, *data_scan;
  3475.   int i;
  3476.   char *errfmt, *file_name;
  3477.   int line_number;
  3478.   int dont_allow_symbol_name;
  3479.  
  3480.   bzero (nlist_bitvector, (number_of_syms >> 3) + 1);
  3481.  
  3482.   /* Read in the files strings if they aren't available */
  3483.   if (!entry->strings)
  3484.     {
  3485.       int desc;
  3486.  
  3487.       entry->strings = (char *) alloca (entry->string_size);
  3488.       desc = file_open (entry);
  3489.       read_entry_strings (desc, entry);
  3490.     }
  3491.  
  3492.   read_file_relocation (entry);
  3493.  
  3494.   /* Do text warnings based on a scan through the relocation info.  */
  3495.   do_relocation_warnings (entry, 0, outfile, nlist_bitvector);
  3496.  
  3497.   /* Do data warnings based on a scan through the relocation info.  */
  3498.   do_relocation_warnings (entry, 1, outfile, nlist_bitvector);
  3499.  
  3500.   /* Scan through all of the nlist entries in this file and pick up
  3501.      anything that the scan through the relocation stuff didn't.  */
  3502.  
  3503.   text_scan = init_debug_scan (0, entry);
  3504.   data_scan = init_debug_scan (1, entry);
  3505.  
  3506.   for (i = 0; i < number_of_syms; i++)
  3507.     {
  3508.       struct nlist *s;
  3509.       struct glosym *g;
  3510.  
  3511.       s = entry->symbols + i;
  3512.  
  3513.       if (!(s->n_type & N_EXT))
  3514.     continue;
  3515.  
  3516.       g = (symbol *) s->n_un.n_name;
  3517.       dont_allow_symbol_name = 0;
  3518.  
  3519.       if (list_multiple_defs && g->multiply_defined)
  3520.     {
  3521.       errfmt = "Definition of symbol %s (multiply defined)";
  3522.       switch (s->n_type)
  3523.         {
  3524.         case N_TEXT | N_EXT:
  3525.           line_number = address_to_line (s->n_value, text_scan);
  3526.           file_name = text_scan[0].filename;
  3527.           break;
  3528.         case N_DATA | N_EXT:
  3529.           line_number = address_to_line (s->n_value, data_scan);
  3530.           file_name = data_scan[0].filename;
  3531.           break;
  3532.         case N_SETA | N_EXT:
  3533.         case N_SETT | N_EXT:
  3534.         case N_SETD | N_EXT:
  3535.         case N_SETB | N_EXT:
  3536.           if (g->multiply_defined == 2)
  3537.         continue;
  3538.           errfmt = "First set element definition of symbol %s (multiply defined)";
  3539.           break;
  3540.         default:
  3541.           continue;        /* Don't print out multiple defs
  3542.                    at references.  */
  3543.         }
  3544.     }
  3545.       else if (BIT_SET_P (nlist_bitvector, i))
  3546.     continue;
  3547.       else if (list_unresolved_refs && !g->defined)
  3548.     {
  3549.       if (g->undef_refs >= MAX_UREFS_PRINTED)
  3550.         continue;
  3551.  
  3552.       if (++(g->undef_refs) == MAX_UREFS_PRINTED)
  3553.         errfmt = "More undefined \"%s\" refs follow";
  3554.       else
  3555.         errfmt = "Undefined symbol \"%s\" referenced";
  3556.       line_number = -1;
  3557.     }
  3558.       else if (g->warning)
  3559.     {
  3560.       /* There are two cases in which we don't want to
  3561.          do this.  The first is if this is a definition instead of
  3562.          a reference.  The second is if it's the reference used by
  3563.          the warning stabs itself.  */
  3564.       if (s->n_type != (N_EXT | N_UNDF)
  3565.           || (i && (s-1)->n_type == N_WARNING))
  3566.         continue;
  3567.  
  3568.       errfmt = g->warning;
  3569.       line_number = -1;
  3570.       dont_allow_symbol_name = 1;
  3571.     }
  3572.       else
  3573.     continue;
  3574.  
  3575.       if (line_number == -1)
  3576.     fprintf (outfile, "%s: ", entry->filename);
  3577.       else
  3578.     fprintf (outfile, "%s:%d: ", file_name, line_number);
  3579.  
  3580.       if (dont_allow_symbol_name)
  3581.       fprintf (outfile, "%s", errfmt);
  3582.       else
  3583.       {
  3584.     char *nm;
  3585.     if (demangler != NULL && (nm = (*demangler)(g->name)) != NULL)
  3586.       {
  3587.         fprintf (outfile, errfmt, nm);
  3588.         free (nm);
  3589.       } else
  3590.         fprintf (outfile, errfmt, g->name);
  3591.       }
  3592.  
  3593.       fputc ('\n', outfile);
  3594.     }
  3595.   free (text_scan);
  3596.   free (data_scan);
  3597.   entry->strings = 0;        /* Since it will dissapear anyway.  */
  3598. }
  3599.  
  3600. do_warnings (outfile)
  3601.      FILE *outfile;
  3602. {
  3603.   int i;
  3604.  
  3605.   list_unresolved_refs = !relocatable_output && undefined_global_sym_count;
  3606.   list_warning_symbols = warning_count;
  3607.   list_multiple_defs = multiple_def_count != 0;
  3608.  
  3609.   if (!(list_unresolved_refs ||
  3610.     list_warning_symbols ||
  3611.     list_multiple_defs      ))
  3612.     /* No need to run this routine */
  3613.     return;
  3614.  
  3615.   each_file (do_file_warnings, outfile);
  3616.   if (trace_files)
  3617.       printf("undefined_global_sym_count= %d, multiple_def_count= %d\n",
  3618.          undefined_global_sym_count, multiple_def_count);
  3619.   if (list_unresolved_refs || list_multiple_defs)
  3620.     make_executable = 0;
  3621. }
  3622.  
  3623. /* Write the output file */
  3624.  
  3625. void
  3626. write_output ()
  3627. {
  3628.   struct stat statbuf;
  3629.   int filemode;
  3630.  
  3631.   outdesc = open (output_filename, O_WRONLY | O_CREAT | O_TRUNC, 0666);
  3632.   if (outdesc < 0) perror_name (output_filename);
  3633.  
  3634.   if (fstat (outdesc, &statbuf) < 0)
  3635.     perror_name (output_filename);
  3636.  
  3637.   filemode = statbuf.st_mode;
  3638.  
  3639.   chmod (output_filename, filemode & ~0111);
  3640.  
  3641.   /* Output the a.out header.  */
  3642.   write_header ();
  3643.  
  3644.   /* Output the text and data segments, relocating as we go.  */
  3645.   write_text ();
  3646.   write_data ();
  3647.  
  3648.   /* Output the merged relocation info, if requested with `-r'.  */
  3649.   if (relocatable_output)
  3650.     write_rel ();
  3651.  
  3652.   /* Output the symbol table (both globals and locals).  */
  3653.   write_syms ();
  3654.  
  3655.   /* Copy any GDB symbol segments from input files.  */
  3656.   write_symsegs ();
  3657.  
  3658.   close (outdesc);
  3659.  
  3660.   if (chmod (output_filename, filemode | 0111) == -1)
  3661.     perror_name (output_filename);
  3662. }
  3663.  
  3664. void modify_location (), perform_relocation (), copy_text (), copy_data ();
  3665.  
  3666. void
  3667. write_header ()
  3668. {
  3669.   N_SET_MAGIC (outheader, magic);
  3670.   outheader.a_text = text_size;
  3671.   if (T_flag_specified)
  3672.       outheader.a_text += text_start;
  3673. #if TARGET_MACHINE==TARGET_SEQUENT
  3674.   outheader.a_text += N_ADDRADJ (outheader);
  3675.   if (entry_symbol == 0)
  3676.       entry_symbol = getsym("start");
  3677. #endif
  3678.   outheader.a_data = data_size;
  3679.   outheader.a_bss = bss_size;
  3680.   outheader.a_entry = (entry_symbol ? entry_symbol->value
  3681.                : text_start + entry_offset);
  3682. #ifdef COFF_ENCAPSULATE
  3683.   if (need_coff_header)
  3684.     {
  3685.       /* We are encapsulating BSD format within COFF format.  */
  3686.       struct coffscn *tp, *dp, *bp;
  3687.  
  3688.       tp = &coffheader.scns[0];
  3689.       dp = &coffheader.scns[1];
  3690.       bp = &coffheader.scns[2];
  3691.  
  3692.       strcpy (tp->s_name, ".text");
  3693.       tp->s_paddr = text_start;
  3694.       tp->s_vaddr = text_start;
  3695.       tp->s_size = text_size;
  3696.       tp->s_scnptr = sizeof (struct coffheader) + sizeof (struct exec);
  3697.       tp->s_relptr = 0;
  3698.       tp->s_lnnoptr = 0;
  3699.       tp->s_nreloc = 0;
  3700.       tp->s_nlnno = 0;
  3701.       tp->s_flags = 0x20;
  3702.       strcpy (dp->s_name, ".data");
  3703.       dp->s_paddr = data_start;
  3704.       dp->s_vaddr = data_start;
  3705.       dp->s_size = data_size;
  3706.       dp->s_scnptr = tp->s_scnptr + tp->s_size;
  3707.       dp->s_relptr = 0;
  3708.       dp->s_lnnoptr = 0;
  3709.       dp->s_nreloc = 0;
  3710.       dp->s_nlnno = 0;
  3711.       dp->s_flags = 0x40;
  3712.       strcpy (bp->s_name, ".bss");
  3713.       bp->s_paddr = dp->s_vaddr + dp->s_size;
  3714.       bp->s_vaddr = bp->s_paddr;
  3715.       bp->s_size = bss_size;
  3716.       bp->s_scnptr = 0;
  3717.       bp->s_relptr = 0;
  3718.       bp->s_lnnoptr = 0;
  3719.       bp->s_nreloc = 0;
  3720.       bp->s_nlnno = 0;
  3721.       bp->s_flags = 0x80;
  3722.  
  3723.       coffheader.f_magic = COFF_MAGIC;
  3724.       coffheader.f_nscns = 3;
  3725.       /* store an unlikely time so programs can
  3726.        * tell that there is a bsd header
  3727.        */
  3728.       coffheader.f_timdat = 1;
  3729.       coffheader.f_symptr = 0;
  3730.       coffheader.f_nsyms = 0;
  3731.       coffheader.f_opthdr = 28;
  3732.       coffheader.f_flags = 0x103;
  3733.       /* aouthdr */
  3734.       coffheader.magic = ZMAGIC;
  3735.       coffheader.vstamp = 0;
  3736.       coffheader.tsize = tp->s_size;
  3737.       coffheader.dsize = dp->s_size;
  3738.       coffheader.bsize = bp->s_size;
  3739.       coffheader.entry = outheader.a_entry;
  3740.       coffheader.text_start = tp->s_vaddr;
  3741.       coffheader.data_start = dp->s_vaddr;
  3742.     }
  3743. #endif
  3744. #if TARGET==TARGET_SEQUENT
  3745.     if (SMAGIC == magic) {
  3746.     char *target_ptr; 
  3747.     long n; 
  3748.  
  3749.     outheader.a_gdtbl = gdt_filler; 
  3750.     if (sizeof(instr) > sizeof(outheader.a_bootstrap)) {
  3751.         fatal("a_bootstrap too small for code"); 
  3752.     } 
  3753.     n = outheader.a_entry; 
  3754.     target_ptr = &instr[sizeof(instr) - 4];
  3755.     target_ptr[0] = n & 0xff; 
  3756.     target_ptr[1] = (n >> 8) & 0xff; 
  3757.     target_ptr[2] = (n >> 16) & 0xff; 
  3758.     target_ptr[3] = (n >> 24) & 0xff; 
  3759.     bcopy(instr, (char *)outheader.a_bootstrap, sizeof(instr)); 
  3760.     }
  3761. #endif
  3762.  
  3763. #ifdef INITIALIZE_HEADER
  3764.   INITIALIZE_HEADER;
  3765. #endif
  3766.  
  3767.   if (strip_symbols == STRIP_ALL)
  3768.     nsyms = 0;
  3769.   else
  3770.     {
  3771.       nsyms = (defined_global_sym_count
  3772.            + undefined_global_sym_count);
  3773.       if (discard_locals == DISCARD_L)
  3774.     nsyms += non_L_local_sym_count;
  3775.       else if (discard_locals == DISCARD_NONE)
  3776.     nsyms += local_sym_count;
  3777.       /* One extra for following reference on indirects */
  3778.       if (relocatable_output)
  3779.     nsyms += set_symbol_count + global_indirect_count;
  3780.     }
  3781.  
  3782.   if (strip_symbols == STRIP_NONE)
  3783.     nsyms += debugger_sym_count;
  3784.  
  3785.   outheader.a_syms = nsyms * sizeof (struct nlist);
  3786.  
  3787.   if (relocatable_output)
  3788.     {
  3789.       outheader.a_trsize = text_reloc_size;
  3790.       outheader.a_drsize = data_reloc_size;
  3791.     }
  3792.   else
  3793.     {
  3794.       outheader.a_trsize = 0;
  3795.       outheader.a_drsize = 0;
  3796.     }
  3797.  
  3798. #ifdef COFF_ENCAPSULATE
  3799.   if (need_coff_header)
  3800.     mywrite (&coffheader, sizeof coffheader, 1, outdesc);
  3801. #endif
  3802. #if HOST_BYTE_ORDER != TARGET_BYTE_ORDER
  3803.   fix_exec_header_byte_order(&outheader);
  3804. #endif
  3805.   mywrite (&outheader, sizeof (struct exec), 1, outdesc);
  3806. #if HOST_BYTE_ORDER != TARGET_BYTE_ORDER
  3807.   fix_exec_header_byte_order(&outheader);
  3808. #endif
  3809.  
  3810.   /* Output whatever padding is required in the executable file
  3811.      between the header and the start of the text.  */
  3812.  
  3813. #ifndef COFF_ENCAPSULATE
  3814.   padfile (N_TXTOFF (outheader) - sizeof outheader, outdesc);
  3815. #endif
  3816.  
  3817.  
  3818.   if (T_flag_specified)
  3819.       padfile (text_start - sizeof(struct exec), outdesc);
  3820.  
  3821. }
  3822.  
  3823. /* Relocate the text segment of each input file
  3824.    and write to the output file.  */
  3825.  
  3826. void
  3827. write_text ()
  3828. {
  3829.   if (trace_files)
  3830.     fprintf (stderr, "Copying and relocating text:\n\n");
  3831.  
  3832.   each_full_file (copy_text);
  3833.   file_close ();
  3834.  
  3835.   if (trace_files)
  3836.     fprintf (stderr, "\n");
  3837.  
  3838.   padfile (text_pad, outdesc);
  3839. }
  3840.  
  3841. int
  3842. text_offset (entry)
  3843.      struct file_entry *entry;
  3844. {
  3845.   return entry->starting_offset + N_TXTOFF (entry->header);
  3846. }
  3847.  
  3848. /* Read in all of the relocation information */
  3849.  
  3850. void
  3851. read_relocation ()
  3852. {
  3853.   each_full_file (read_file_relocation);
  3854. }
  3855.  
  3856. /* Read in the relocation sections of ENTRY if necessary */
  3857.  
  3858. void
  3859. read_file_relocation (entry)
  3860.      struct file_entry *entry;
  3861. {
  3862.   register struct relocation_info *reloc;
  3863.   int desc;
  3864.   int read_return;
  3865.  
  3866.   desc = -1;
  3867.   if (!entry->textrel)
  3868.     {
  3869.       reloc = (struct relocation_info *) xmalloc (entry->header.a_trsize);
  3870.       desc = file_open (entry);
  3871.       lseek (desc,
  3872.          text_offset (entry) + entry->header.a_text + entry->header.a_data,
  3873.          L_SET);
  3874.       if (entry->header.a_trsize != (read_return = read (desc, reloc, entry->header.a_trsize)))
  3875.     {
  3876.       fprintf (stderr, "Return from read: %d\n", read_return);
  3877.       fatal_with_file ("premature eof in text relocation of ", entry);
  3878.     }
  3879. #if HOST_BYTE_ORDER != TARGET_BYTE_ORDER
  3880.       target_to_host_reloc_byte_order(reloc,
  3881.     entry->header.a_trsize/sizeof(*reloc));
  3882. #endif
  3883.       entry->textrel = reloc;
  3884.     }
  3885.  
  3886.   if (!entry->datarel)
  3887.     {
  3888.       reloc = (struct relocation_info *) xmalloc (entry->header.a_drsize);
  3889.       if (desc == -1) desc = file_open (entry);
  3890.       lseek (desc,
  3891.          text_offset (entry) + entry->header.a_text
  3892.          + entry->header.a_data + entry->header.a_trsize,
  3893.          L_SET);
  3894.       if (entry->header.a_drsize != read (desc, reloc, entry->header.a_drsize))
  3895.     fatal_with_file ("premature eof in data relocation of ", entry);
  3896. #if HOST_BYTE_ORDER != TARGET_BYTE_ORDER
  3897.       target_to_host_reloc_byte_order(reloc,
  3898.     entry->header.a_drsize/sizeof(*reloc));
  3899. #endif
  3900.       entry->datarel = reloc;
  3901.     }
  3902. }
  3903.  
  3904. /* Read the text segment contents of ENTRY, relocate them,
  3905.    and write the result to the output file.
  3906.    If `-r', save the text relocation for later reuse.  */
  3907.  
  3908. void
  3909. copy_text (entry)
  3910.      struct file_entry *entry;
  3911. {
  3912.   register char *bytes;
  3913.   register int desc;
  3914.   register struct relocation_info *reloc;
  3915.  
  3916.   if (trace_files)
  3917.     prline_file_name (entry, stderr);
  3918.  
  3919.   desc = file_open (entry);
  3920.  
  3921.   /* Allocate space for the file's text section */
  3922.  
  3923.   bytes = (char *) alloca (entry->header.a_text);
  3924.  
  3925.   /* Deal with relocation information however is appropriate */
  3926.  
  3927.   if (entry->textrel)  reloc = entry->textrel;
  3928.   else if (relocatable_output)
  3929.     {
  3930.       read_file_relocation (entry);
  3931.       reloc = entry->textrel;
  3932.     }
  3933.   else
  3934.     {
  3935.       reloc = (struct relocation_info *) alloca (entry->header.a_trsize);
  3936.       if (trace_files)
  3937.           printf("lseek to 0x%x (t_o= 0x%x, a_t= 0x%x, a_d= 0x%x)\n",
  3938.              text_offset(entry) + entry->header.a_text +
  3939.              entry->header.a_data, text_offset(entry),
  3940.              entry->header.a_text, entry->header.a_data);
  3941.       lseek (desc, text_offset (entry) + entry->header.a_text + entry->header.a_data, 0);
  3942.       if (entry->header.a_trsize != read (desc, reloc, entry->header.a_trsize))
  3943.     fatal_with_file ("premature eof in text relocation of ", entry);
  3944. #if HOST_BYTE_ORDER != TARGET_BYTE_ORDER
  3945.       target_to_host_reloc_byte_order(reloc,
  3946.       entry->header.a_trsize/sizeof(*reloc));
  3947. #endif
  3948.     }
  3949.  
  3950.   /* Read the text section into core.  */
  3951.  
  3952.   lseek (desc, text_offset (entry), 0);
  3953.   if (entry->header.a_text != read (desc, bytes, entry->header.a_text))
  3954.     fatal_with_file ("premature eof in text section of ", entry);
  3955.  
  3956.  
  3957.   /* Relocate the text according to the text relocation.  */
  3958.  
  3959.   perform_relocation (bytes, entry->text_start_address, entry->header.a_text,
  3960.               reloc, entry->header.a_trsize, entry);
  3961.  
  3962.   /* Write the relocated text to the output file.  */
  3963.  
  3964.   mywrite (bytes, 1, entry->header.a_text, outdesc);
  3965. }
  3966.  
  3967. /* Relocate the data segment of each input file
  3968.    and write to the output file.  */
  3969.  
  3970. void
  3971. write_data ()
  3972. {
  3973.   if (trace_files)
  3974.     fprintf (stderr, "Copying and relocating data:\n\n");
  3975.  
  3976.   each_full_file (copy_data);
  3977.   file_close ();
  3978.  
  3979.   /* Write out the set element vectors.  See digest symbols for
  3980.      description of length of the set vector section.  */
  3981.  
  3982. #if HOST_BYTE_ORDER != TARGET_BYTE_ORDER
  3983.   {
  3984.     int i;
  3985.     for (i = 0; i < 2 * set_symbol_count + set_vector_count; ++i) {
  3986.     fix_byte_order(&set_vectors[i], sizeof(*set_vectors));
  3987.     }
  3988.   }
  3989. #endif
  3990.  
  3991.   if (set_vector_count)
  3992.     mywrite (set_vectors, 2 * set_symbol_count + set_vector_count,
  3993.          sizeof (unsigned long), outdesc);
  3994.  
  3995.   if (trace_files)
  3996.     fprintf (stderr, "\n");
  3997.  
  3998.   padfile (data_pad, outdesc);
  3999. }
  4000.  
  4001. /* Read the data segment contents of ENTRY, relocate them,
  4002.    and write the result to the output file.
  4003.    If `-r', save the data relocation for later reuse.
  4004.    See comments in `copy_text'.  */
  4005.  
  4006. void
  4007. copy_data (entry)
  4008.      struct file_entry *entry;
  4009. {
  4010.   register struct relocation_info *reloc;
  4011.   register char *bytes;
  4012.   register int desc;
  4013.  
  4014.   if (trace_files)
  4015.     prline_file_name (entry, stderr);
  4016.  
  4017.   desc = file_open (entry);
  4018.  
  4019.   bytes = (char *) alloca (entry->header.a_data);
  4020.  
  4021.   if (entry->datarel) reloc = entry->datarel;
  4022.   else if (relocatable_output)    /* Will need this again */
  4023.     {
  4024.       read_file_relocation (entry);
  4025.       reloc = entry->datarel;
  4026.     }
  4027.   else
  4028.     {
  4029.       reloc = (struct relocation_info *) alloca (entry->header.a_drsize);
  4030.       lseek (desc, text_offset (entry) + entry->header.a_text
  4031.          + entry->header.a_data + entry->header.a_trsize,
  4032.          0);
  4033.       if (entry->header.a_drsize != read (desc, reloc, entry->header.a_drsize))
  4034.     fatal_with_file ("premature eof in data relocation of ", entry);
  4035. #if HOST_BYTE_ORDER != TARGET_BYTE_ORDER
  4036.       target_to_host_reloc_byte_order(reloc,
  4037.       entry->header.a_drsize/sizeof(*reloc));
  4038. #endif
  4039.     }
  4040.  
  4041.   lseek (desc, text_offset (entry) + entry->header.a_text, 0);
  4042.   if (entry->header.a_data != read (desc, bytes, entry->header.a_data))
  4043.     fatal_with_file ("premature eof in data section of ", entry);
  4044.  
  4045.   perform_relocation (bytes, entry->data_start_address - entry->header.a_text,
  4046.               entry->header.a_data, reloc, entry->header.a_drsize, entry);
  4047.  
  4048.   mywrite (bytes, 1, entry->header.a_data, outdesc);
  4049. }
  4050.  
  4051. /* Relocate ENTRY's text or data section contents.
  4052.    DATA is the address of the contents, in core.
  4053.    DATA_SIZE is the length of the contents.
  4054.    PC_RELOCATION is the difference between the address of the contents
  4055.      in the output file and its address in the input file.
  4056.    RELOC_INFO is the address of the relocation info, in core.
  4057.    RELOC_SIZE is its length in bytes.  */
  4058. /* This version is about to be severley hacked by Randy.  Hope it
  4059.    works afterwards. */
  4060. void
  4061. perform_relocation (data, pc_relocation, data_size, reloc_info, reloc_size, entry)
  4062.      char *data;
  4063.      struct relocation_info *reloc_info;
  4064.      struct file_entry *entry;
  4065.      int pc_relocation;
  4066.      int data_size;
  4067.      int reloc_size;
  4068. {
  4069.   register struct relocation_info *p = reloc_info;
  4070.   struct relocation_info *end
  4071.     = reloc_info + reloc_size / sizeof (struct relocation_info);
  4072.   int text_relocation = entry->text_start_address;
  4073.   int data_relocation = entry->data_start_address - entry->header.a_text;
  4074.   int bss_relocation
  4075.     = entry->bss_start_address - entry->header.a_text - entry->header.a_data;
  4076.  
  4077.   for (; p < end; p++)
  4078.     {
  4079.       register int relocation = 0;
  4080.       register int addr = RELOC_ADDRESS(p);
  4081.       register unsigned int mask = 0;
  4082.       register unsigned int x;
  4083.  
  4084.       if (addr >= data_size)
  4085.     fatal_with_file ("relocation address out of range in ", entry);
  4086.  
  4087.       if (RELOC_EXTERN_P(p))
  4088.     {
  4089.       int symindex = RELOC_SYMBOL (p) * sizeof (struct nlist);
  4090.       symbol *sp = ((symbol *)
  4091.             (((struct nlist *)
  4092.               (((char *)entry->symbols) + symindex))
  4093.              ->n_un.n_name));
  4094.  
  4095. #ifdef N_INDR
  4096.       /* Resolve indirection */
  4097.       if ((sp->defined & ~N_EXT) == N_INDR)
  4098.         sp = (symbol *) sp->value;
  4099. #endif
  4100.  
  4101.       if (symindex >= entry->header.a_syms)
  4102.         fatal_with_file ("relocation symbolnum out of range in ", entry);
  4103.  
  4104.       /* If the symbol is undefined, leave it at zero.  */
  4105.       if (! sp->defined)
  4106.         relocation = 0;
  4107.       else
  4108.         relocation = sp->value;
  4109.     }
  4110.       else switch (RELOC_TYPE(p))
  4111.     {
  4112.     case N_TEXT:
  4113.     case N_TEXT | N_EXT:
  4114.       relocation = text_relocation;
  4115.       break;
  4116.  
  4117.     case N_DATA:
  4118.     case N_DATA | N_EXT:
  4119.       /* A word that points to beginning of the the data section
  4120.          initially contains not 0 but rather the "address" of that section
  4121.          in the input file, which is the length of the file's text.  */
  4122.       relocation = data_relocation;
  4123.       break;
  4124.  
  4125.     case N_BSS:
  4126.     case N_BSS | N_EXT:
  4127.       /* Similarly, an input word pointing to the beginning of the bss
  4128.          initially contains the length of text plus data of the file.  */
  4129.       relocation = bss_relocation;
  4130.       break;
  4131.  
  4132.     case N_ABS:
  4133.     case N_ABS | N_EXT:
  4134.       /* Don't know why this code would occur, but apparently it does.  */
  4135.       break;
  4136.  
  4137.     default:
  4138.       fatal_with_file ("nonexternal relocation code invalid in ", entry);
  4139.     }
  4140. #if 0
  4141.       if (RELOC_PCREL_P(p))
  4142.     relocation -= pc_relocation;
  4143. #endif
  4144.  
  4145. #ifdef RELOC_ADD_EXTRA
  4146.       relocation += RELOC_ADD_EXTRA(p);
  4147.       if (relocatable_output)
  4148.     {
  4149.       /* If this RELOC_ADD_EXTRA is 0, it means that the
  4150.          symbol was external and the relocation does not
  4151.          need a fixup here.  */
  4152.       if (RELOC_ADD_EXTRA (p))
  4153.         {
  4154.           if (! RELOC_PCREL_P (p))
  4155.         RELOC_ADD_EXTRA (p) = relocation;
  4156.           else
  4157.         RELOC_ADD_EXTRA (p) -= pc_relocation;
  4158.         }
  4159. #if 1
  4160.       if (! RELOC_PCREL_P (p))
  4161.         {
  4162.           if ((int)p->r_type <= (int) RELOC_32
  4163.           || RELOC_EXTERN_P (p) == 0)
  4164.         RELOC_ADD_EXTRA (p) = relocation;
  4165.         }
  4166.       else if (RELOC_EXTERN_P (p))
  4167.         RELOC_ADD_EXTRA (p) -= pc_relocation;
  4168. #endif
  4169.       continue;
  4170.     }
  4171. #endif
  4172. #if 1    
  4173.        if (RELOC_PCREL_P(p))
  4174.        relocation -= pc_relocation;
  4175. #endif
  4176.  
  4177.       relocation >>= RELOC_VALUE_RIGHTSHIFT(p);
  4178.  
  4179.       /* Unshifted mask for relocation */
  4180.       mask = 1 << RELOC_TARGET_BITSIZE(p) - 1;
  4181.       mask |= mask - 1;
  4182.       relocation &= mask;
  4183.  
  4184.       /* Shift everything up to where it's going to be used */
  4185.       relocation <<= RELOC_TARGET_BITPOS(p);
  4186.       mask <<= RELOC_TARGET_BITPOS(p);
  4187.  
  4188.       switch (RELOC_TARGET_SIZE(p))
  4189.     {
  4190.     case 0:
  4191.       if (RELOC_MEMORY_SUB_P(p))
  4192.         relocation -= mask & *(char *) (data + addr);
  4193.       else if (RELOC_MEMORY_ADD_P(p))
  4194.         relocation += mask & *(char *) (data + addr);
  4195.       *(char *) (data + addr) &= ~mask;
  4196.       *(char *) (data + addr) |= relocation;
  4197.       break;
  4198.  
  4199.     case 1:
  4200. #if TARGET_BYTE_ORDER==BIG_ENDIAN
  4201.       x = ((unsigned char *) (data + addr))[1]
  4202.        | (((unsigned char *) (data + addr))[0] << 8);
  4203. #endif
  4204. #if TARGET_BYTE_ORDER==LITTLE_ENDIAN
  4205.       x = ((unsigned char *) (data + addr))[0]
  4206.        | (((unsigned char *) (data + addr))[1] << 8);
  4207. #endif
  4208.       if (RELOC_MEMORY_SUB_P(p))
  4209.         relocation -= mask & x;
  4210.       else if (RELOC_MEMORY_ADD_P(p))
  4211.         relocation += mask & x;
  4212.       x &= ~mask;
  4213.       x |= relocation;
  4214. #if TARGET_BYTE_ORDER==BIG_ENDIAN
  4215.       ((unsigned char *) (data + addr))[1] = x;
  4216.       ((unsigned char *) (data + addr))[0] = x >> 8;
  4217. #endif
  4218. #if TARGET_BYTE_ORDER==LITTLE_ENDIAN
  4219.       ((unsigned char *) (data + addr))[0] = x;
  4220.       ((unsigned char *) (data + addr))[1] = x >> 8;
  4221. #endif
  4222.       break;
  4223.  
  4224.     case 2:
  4225. #if TARGET_BYTE_ORDER==BIG_ENDIAN
  4226.       x = ((unsigned char *) (data + addr))[3]
  4227.        | (((unsigned char *) (data + addr))[2] << 8)
  4228.        | (((unsigned char *) (data + addr))[1] << 16)
  4229.        | (((unsigned char *) (data + addr))[0] << 24);
  4230. #endif
  4231. #if TARGET_BYTE_ORDER==LITTLE_ENDIAN
  4232.       x = ((unsigned char *) (data + addr))[0]
  4233.        | (((unsigned char *) (data + addr))[1] << 8)
  4234.        | (((unsigned char *) (data + addr))[2] << 16)
  4235.        | (((unsigned char *) (data + addr))[3] << 24);
  4236. #endif
  4237.           if (RELOC_MEMORY_SUB_P(p))
  4238.         relocation -= mask & x;
  4239.       else if (RELOC_MEMORY_ADD_P(p))
  4240.         relocation += mask & x;
  4241.       x &= ~mask;
  4242.       x |= relocation;
  4243. #if TARGET_BYTE_ORDER==BIG_ENDIAN
  4244.       ((unsigned char *) (data + addr))[3] = x;
  4245.       ((unsigned char *) (data + addr))[2] = x >> 8;
  4246.       ((unsigned char *) (data + addr))[1] = x >> 16;
  4247.       ((unsigned char *) (data + addr))[0] = x >> 24;
  4248. #endif
  4249. #if TARGET_BYTE_ORDER==LITTLE_ENDIAN
  4250.       ((unsigned char *) (data + addr))[0] = x;
  4251.       ((unsigned char *) (data + addr))[1] = x >> 8;
  4252.       ((unsigned char *) (data + addr))[2] = x >> 16;
  4253.       ((unsigned char *) (data + addr))[3] = x >> 24;
  4254. #endif
  4255.       break;
  4256.  
  4257.     default:
  4258.       fatal_with_file ("Unimplemented relocation field length in ", entry);
  4259.     }
  4260.     }
  4261. }
  4262.  
  4263. /* For relocatable_output only: write out the relocation,
  4264.    relocating the addresses-to-be-relocated.  */
  4265.  
  4266. void coptxtrel (), copdatrel ();
  4267.  
  4268. void
  4269. write_rel ()
  4270. {
  4271.   register int i;
  4272.   register int count = 0;
  4273.  
  4274.   if (trace_files)
  4275.     fprintf (stderr, "Writing text relocation:\n\n");
  4276.  
  4277.   /* Assign each global symbol a sequence number, giving the order
  4278.      in which `write_syms' will write it.
  4279.      This is so we can store the proper symbolnum fields
  4280.      in relocation entries we write.  */
  4281.  
  4282.   for (i = 0; i < TABSIZE; i++)
  4283.     {
  4284.       symbol *sp;
  4285.       for (sp = symtab[i]; sp; sp = sp->link)
  4286.     if (sp->referenced || sp->defined)
  4287.       {
  4288.         sp->def_count = count++;
  4289.         /* Leave room for the reference required by N_INDR, if
  4290.            necessary.  */
  4291.         if ((sp->defined & ~N_EXT) == N_INDR)
  4292.           count++;
  4293.       }
  4294.     }
  4295.   /* Correct, because if (reloatable_output), we will also be writing
  4296.      whatever indirect blocks we have.  */
  4297.   if (count != defined_global_sym_count
  4298.       + undefined_global_sym_count + global_indirect_count)
  4299.     fatal ("internal error");
  4300.  
  4301.   /* Write out the relocations of all files, remembered from copy_text.  */
  4302.  
  4303.   each_full_file (coptxtrel);
  4304.  
  4305.   if (trace_files)
  4306.     fprintf (stderr, "\nWriting data relocation:\n\n");
  4307.  
  4308.   each_full_file (copdatrel);
  4309.  
  4310.   if (trace_files)
  4311.     fprintf (stderr, "\n");
  4312. }
  4313.  
  4314. void
  4315. coptxtrel (entry)
  4316.      struct file_entry *entry;
  4317. {
  4318.   register struct relocation_info *p, *end;
  4319.   register int reloc = entry->text_start_address;
  4320.  
  4321.   p = entry->textrel;
  4322.   end = (struct relocation_info *) (entry->header.a_trsize + (char *) p);
  4323.   while (p < end)
  4324.     {
  4325.       RELOC_ADDRESS(p) += reloc;
  4326.       if (RELOC_EXTERN_P(p))
  4327.     {
  4328.       register int symindex = RELOC_SYMBOL(p) * sizeof (struct nlist);
  4329.       symbol *symptr = ((symbol *)
  4330.                 (((struct nlist *)
  4331.                   (((char *)entry->symbols) + symindex))
  4332.                  ->n_un.n_name));
  4333.  
  4334.       if (symindex >= entry->header.a_syms)
  4335.         fatal_with_file ("relocation symbolnum out of range in ", entry);
  4336.  
  4337. #ifdef N_INDR
  4338.       /* Resolve indirection.  */
  4339.       if ((symptr->defined & ~N_EXT) == N_INDR)
  4340.         symptr = (symbol *) symptr->value;
  4341. #endif
  4342.  
  4343.       /* If the symbol is now defined, change the external relocation
  4344.          to an internal one.  */
  4345.  
  4346.       if (symptr->defined)
  4347.         {
  4348.           RELOC_EXTERN_P(p) = 0;
  4349.           RELOC_SYMBOL(p) = (symptr->defined & N_TYPE);
  4350. #ifdef RELOC_ADD_EXTRA
  4351.           /* If we aren't going to be adding in the value in
  4352.              memory on the next pass of the loader, then we need
  4353.          to add it in from the relocation entry.  Otherwise
  4354.              the work we did in this pass is lost.  */
  4355.           if (!RELOC_MEMORY_ADD_P(p))
  4356.         RELOC_ADD_EXTRA (p) += symptr->value;
  4357. #endif
  4358.         }
  4359.       else
  4360.         /* Debugger symbols come first, so have to start this
  4361.            after them.  */
  4362.           RELOC_SYMBOL(p) = (symptr->def_count + nsyms
  4363.                  - defined_global_sym_count
  4364.                  - undefined_global_sym_count
  4365.                  - global_indirect_count);
  4366.     }
  4367.       p++;
  4368.     }
  4369. #if HOST_BYTE_ORDER != TARGET_BYTE_ORDER
  4370.     host_to_target_reloc_byte_order((struct relocation_info *) entry->textrel,
  4371.     entry->header.a_trsize/sizeof(struct relocation_info));
  4372. #endif
  4373.   mywrite (entry->textrel, 1, entry->header.a_trsize, outdesc);
  4374. }
  4375.  
  4376. void
  4377. copdatrel (entry)
  4378.      struct file_entry *entry;
  4379. {
  4380.   register struct relocation_info *p, *end;
  4381.   /* Relocate the address of the relocation.
  4382.      Old address is relative to start of the input file's data section.
  4383.      New address is relative to start of the output file's data section.  */
  4384.   register int reloc = entry->data_start_address - text_size;
  4385.  
  4386.   p = entry->datarel;
  4387.   end = (struct relocation_info *) (entry->header.a_drsize + (char *) p);
  4388.   while (p < end)
  4389.     {
  4390.       RELOC_ADDRESS(p) += reloc;
  4391.       if (RELOC_EXTERN_P(p))
  4392.     {
  4393.       register int symindex = RELOC_SYMBOL(p) * sizeof (struct nlist);
  4394.       symbol *symptr = ((symbol *)
  4395.                 (((struct nlist *)
  4396.                   (((char *)entry->symbols) + symindex))
  4397.                  ->n_un.n_name));
  4398.       int symtype;
  4399.  
  4400.       if (symindex >= entry->header.a_syms)
  4401.         fatal_with_file ("relocation symbolnum out of range in ", entry);
  4402.  
  4403. #ifdef N_INDR
  4404.       /* Resolve indirection.  */
  4405.       if ((symptr->defined & ~N_EXT) == N_INDR)
  4406.         symptr = (symbol *) symptr->value;
  4407. #endif
  4408.  
  4409.        symtype = symptr->defined & N_TYPE;
  4410.  
  4411.       if (force_common_definition
  4412.           || symtype == N_DATA || symtype == N_TEXT || symtype == N_ABS)
  4413.         {
  4414.           RELOC_EXTERN_P(p) = 0;
  4415.           RELOC_SYMBOL(p) = symtype;
  4416.         }
  4417.       else
  4418.         /* Debugger symbols come first, so have to start this
  4419.            after them.  */
  4420.         RELOC_SYMBOL(p)
  4421.           = (((symbol *)
  4422.           (((struct nlist *)
  4423.             (((char *)entry->symbols) + symindex))
  4424.            ->n_un.n_name))
  4425.          ->def_count
  4426.          + nsyms - defined_global_sym_count
  4427.          - undefined_global_sym_count
  4428.          - global_indirect_count);
  4429.     }
  4430.       p++;
  4431.     }
  4432. #if HOST_BYTE_ORDER != TARGET_BYTE_ORDER
  4433.     host_to_target_reloc_byte_order((struct relocation_info *) entry->datarel,
  4434.     entry->header.a_drsize/sizeof(struct relocation_info));
  4435. #endif
  4436.   mywrite (entry->datarel, 1, entry->header.a_drsize, outdesc);
  4437. }
  4438.  
  4439. void write_file_syms ();
  4440. void write_string_table ();
  4441.  
  4442. /* Offsets and current lengths of symbol and string tables in output file. */
  4443.  
  4444. int symbol_table_offset;
  4445. int symbol_table_len;
  4446.  
  4447. /* Address in output file where string table starts.  */
  4448. int string_table_offset;
  4449.  
  4450. /* Offset within string table
  4451.    where the strings in `strtab_vector' should be written.  */
  4452. int string_table_len;
  4453.  
  4454. /* Total size of string table strings allocated so far,
  4455.    including strings in `strtab_vector'.  */
  4456. int strtab_size;
  4457.  
  4458. /* Vector whose elements are strings to be added to the string table.  */
  4459. char **strtab_vector;
  4460.  
  4461. /* Vector whose elements are the lengths of those strings.  */
  4462. int *strtab_lens;
  4463.  
  4464. /* Index in `strtab_vector' at which the next string will be stored.  */
  4465. int strtab_index;
  4466.  
  4467. /* Add the string NAME to the output file string table.
  4468.    Record it in `strtab_vector' to be output later.
  4469.    Return the index within the string table that this string will have.  */
  4470.  
  4471. int
  4472. assign_string_table_index (name)
  4473.      char *name;
  4474. {
  4475.   register int index = strtab_size;
  4476.   register int len = strlen (name) + 1;
  4477.  
  4478.   strtab_size += len;
  4479.   strtab_vector[strtab_index] = name;
  4480.   strtab_lens[strtab_index++] = len;
  4481.  
  4482.   return index;
  4483. }
  4484.  
  4485. FILE *outstream = (FILE *) 0;
  4486.  
  4487. /* Write the contents of `strtab_vector' into the string table.
  4488.    This is done once for each file's local&debugger symbols
  4489.    and once for the global symbols.  */
  4490.  
  4491. void
  4492. write_string_table ()
  4493. {
  4494.   register int i;
  4495.  
  4496.   lseek (outdesc, string_table_offset + string_table_len, 0);
  4497.  
  4498.   if (!outstream)
  4499.     outstream = fdopen (outdesc, "w");
  4500.  
  4501.   for (i = 0; i < strtab_index; i++)
  4502.     {
  4503.       fwrite (strtab_vector[i], 1, strtab_lens[i], outstream);
  4504.       string_table_len += strtab_lens[i];
  4505.     }
  4506.  
  4507.   fflush (outstream);
  4508.  
  4509.   /* Report I/O error such as disk full.  */
  4510.   if (ferror (outstream))
  4511.     perror_name (output_filename);
  4512. }
  4513.  
  4514. /* Write the symbol table and string table of the output file.  */
  4515.  
  4516. void
  4517. write_syms ()
  4518. {
  4519.   /* Number of symbols written so far.  */
  4520.   int syms_written = 0;
  4521.   register int i;
  4522.   register symbol *sp;
  4523.  
  4524.   /* Buffer big enough for all the global symbols.  One
  4525.      extra struct for each indirect symbol to hold the extra reference
  4526.      following. */
  4527.   struct nlist *buf
  4528.     = (struct nlist *) alloca ((defined_global_sym_count
  4529.                 + undefined_global_sym_count
  4530.                 + global_indirect_count)
  4531.                    * sizeof (struct nlist));
  4532.   /* Pointer for storing into BUF.  */
  4533.   register struct nlist *bufp = buf;
  4534.  
  4535.   /* Size of string table includes the bytes that store the size.  */
  4536.   strtab_size = sizeof strtab_size;
  4537.  
  4538.   symbol_table_offset = N_SYMOFF (outheader);
  4539.   symbol_table_len = 0;
  4540.   string_table_offset = N_STROFF (outheader);
  4541.   string_table_len = strtab_size;
  4542.  
  4543.   if (strip_symbols == STRIP_ALL)
  4544.     return;
  4545.  
  4546.   /* Write the local symbols defined by the various files.  */
  4547.  
  4548.   each_file (write_file_syms, &syms_written);
  4549.   file_close ();
  4550.  
  4551.   /* Now write out the global symbols.  */
  4552.  
  4553.   /* Allocate two vectors that record the data to generate the string
  4554.      table from the global symbols written so far.  This must include
  4555.      extra space for the references following indirect outputs. */
  4556.  
  4557.   strtab_vector = (char **) alloca ((num_hash_tab_syms
  4558.                      + global_indirect_count) * sizeof (char *));
  4559.   strtab_lens = (int *) alloca ((num_hash_tab_syms
  4560.                  + global_indirect_count) * sizeof (int));
  4561.   strtab_index = 0;
  4562.  
  4563.   /* Scan the symbol hash table, bucket by bucket.  */
  4564.  
  4565.   for (i = 0; i < TABSIZE; i++)
  4566.     for (sp = symtab[i]; sp; sp = sp->link)
  4567.       {
  4568.     struct nlist nl;
  4569.  
  4570.     nl.n_other = 0;
  4571.     nl.n_desc = 0;
  4572.  
  4573.     /* Compute a `struct nlist' for the symbol.  */
  4574.  
  4575.     if (sp->defined || sp->referenced)
  4576.       {
  4577.         /* common condition needs to be before undefined condition */
  4578.         /* because unallocated commons are set undefined in */
  4579.         /* digest_symbols */
  4580.         if (sp->defined > 1) /* defined with known type */
  4581.           {
  4582.         /* If the target of an indirect symbol has been
  4583.            defined and we are outputting an executable,
  4584.            resolve the indirection; it's no longer needed */
  4585.         if (!relocatable_output
  4586.             && ((sp->defined & N_TYPE) == N_INDR)
  4587.             && (((symbol *) sp->value)->defined > 1))
  4588.           {
  4589.             symbol *newsp = (symbol *) sp->value;
  4590.             nl.n_type = newsp->defined;
  4591.             nl.n_value = newsp->value;
  4592.           }
  4593.         else
  4594.           {
  4595.             nl.n_type = sp->defined;
  4596.             if (sp->defined != (N_INDR | N_EXT))
  4597.               nl.n_value = sp->value;
  4598.             else
  4599.               nl.n_value = 0;
  4600.           }
  4601.           }
  4602.         else if (sp->max_common_size) /* defined as common but not allocated. */
  4603.           {
  4604.         /* happens only with -r and not -d */
  4605.         /* write out a common definition */
  4606.         nl.n_type = N_UNDF | N_EXT;
  4607.         nl.n_value = sp->max_common_size;
  4608.           }
  4609.         else if (!sp->defined)          /* undefined -- legit only if -r */
  4610.           {
  4611.         nl.n_type = N_UNDF | N_EXT;
  4612.         nl.n_value = 0;
  4613.           }
  4614.         else
  4615.           fatal ("internal error: %s defined in mysterious way", sp->name);
  4616.  
  4617.         /* Allocate string table space for the symbol name.  */
  4618.  
  4619.         nl.n_un.n_strx = assign_string_table_index (sp->name);
  4620.  
  4621.         /* Output to the buffer and count it.  */
  4622.  
  4623.         *bufp++ = nl;
  4624.         syms_written++;
  4625.         if (nl.n_type == (N_INDR | N_EXT))
  4626.           {
  4627.         struct nlist xtra_ref;
  4628.         xtra_ref.n_type == N_EXT | N_UNDF;
  4629.         xtra_ref.n_un.n_strx
  4630.           = assign_string_table_index (((symbol *) sp->value)->name);
  4631.         xtra_ref.n_other = 0;
  4632.         xtra_ref.n_desc = 0;
  4633.         xtra_ref.n_value = 0;
  4634.         *bufp++ = xtra_ref;
  4635.         syms_written++;
  4636.           }
  4637.       }
  4638.       }
  4639.  
  4640.   /* Output the buffer full of `struct nlist's.  */
  4641.  
  4642.   lseek (outdesc, symbol_table_offset + symbol_table_len, 0);
  4643. #if HOST_BYTE_ORDER != TARGET_BYTE_ORDER
  4644.   {
  4645.     int i;
  4646.  
  4647.     for (i = 0; i < bufp - buf; ++i)
  4648.       {
  4649.         fix_byte_order(&buf[i].n_un.n_name, sizeof(buf->n_un.n_name));
  4650.     fix_byte_order(&buf[i].n_desc, sizeof(buf->n_desc));
  4651.     fix_byte_order(&buf[i].n_value, sizeof(buf->n_value));
  4652.       }
  4653.   }
  4654. #endif
  4655.   mywrite (buf, sizeof (struct nlist), bufp - buf, outdesc);
  4656.   symbol_table_len += sizeof (struct nlist) * (bufp - buf);
  4657.  
  4658.   if (syms_written != nsyms)
  4659.     fatal ("internal error: wrong number of symbols written into output file", 0);
  4660.  
  4661.   if (symbol_table_offset + symbol_table_len != string_table_offset)
  4662.     fatal ("internal error: inconsistent symbol table length", 0);
  4663.  
  4664.   /* Now the total string table size is known, so write it.
  4665.      We are already positioned at the right place in the file.  */
  4666.  
  4667. #if HOST_BYTE_ORDER != TARGET_BYTE_ORDER
  4668.   fix_byte_order(&strtab_size, sizeof(strtab_size));
  4669. #endif
  4670.  
  4671.   mywrite (&strtab_size, sizeof (int), 1, outdesc);  /* we're at right place */
  4672.  
  4673.   /* Write the strings for the global symbols.  */
  4674.  
  4675.   write_string_table ();
  4676. }
  4677.  
  4678. /* Write the local and debugger symbols of file ENTRY.
  4679.    Increment *SYMS_WRITTEN_ADDR for each symbol that is written.  */
  4680.  
  4681. /* Note that we do not combine identical names of local symbols.
  4682.    dbx or gdb would be confused if we did that.  */
  4683.  
  4684. void
  4685. write_file_syms (entry, syms_written_addr)
  4686.      struct file_entry *entry;
  4687.      int *syms_written_addr;
  4688. {
  4689.   register struct nlist *p = entry->symbols;
  4690.   register struct nlist *end = p + entry->header.a_syms / sizeof (struct nlist);
  4691.  
  4692.   /* Buffer to accumulate all the syms before writing them.
  4693.      It has one extra slot for the local symbol we generate here.  */
  4694.   struct nlist *buf
  4695.     = (struct nlist *) alloca (entry->header.a_syms + sizeof (struct nlist));
  4696.   register struct nlist *bufp = buf;
  4697.  
  4698.   /* Upper bound on number of syms to be written here.  */
  4699.   int max_syms = (entry->header.a_syms / sizeof (struct nlist)) + 1;
  4700.  
  4701.   /* Make tables that record, for each symbol, its name and its name's length.
  4702.      The elements are filled in by `assign_string_table_index'.  */
  4703.  
  4704.   strtab_vector = (char **) alloca (max_syms * sizeof (char *));
  4705.   strtab_lens = (int *) alloca (max_syms * sizeof (int));
  4706.   strtab_index = 0;
  4707.  
  4708.   /* Generate a local symbol for the start of this file's text.  */
  4709.  
  4710.   if (discard_locals != DISCARD_ALL)
  4711.     {
  4712.       struct nlist nl;
  4713.  
  4714. #if TARGET==TARGET_SEQUENT      
  4715.       nl.n_type = N_FN;
  4716. #else      
  4717.       nl.n_type = N_TEXT;
  4718. #endif
  4719.       nl.n_un.n_strx = assign_string_table_index (entry->local_sym_name);
  4720.       nl.n_value = entry->text_start_address;
  4721.       nl.n_desc = 0;
  4722.       nl.n_other = 0;
  4723.       *bufp++ = nl;
  4724.       (*syms_written_addr)++;
  4725.       entry->local_syms_offset = *syms_written_addr * sizeof (struct nlist);
  4726.     }
  4727.  
  4728.   /* Read the file's string table.  */
  4729.  
  4730.   entry->strings = (char *) alloca (entry->string_size);
  4731.   read_entry_strings (file_open (entry), entry);
  4732.  
  4733.   for (; p < end; p++)
  4734.     {
  4735.       register int type = p->n_type;
  4736.       register int write = 0;
  4737.  
  4738.       /* WRITE gets 1 for a non-global symbol that should be written.  */
  4739.  
  4740.  
  4741.       if (SET_ELEMENT_P (type))    /* This occurs even if global.  These */
  4742.                 /* types of symbols are never written */
  4743.                 /* globally, though they are stored */
  4744.                 /* globally.  */
  4745.         write = relocatable_output;
  4746.       else if (!(type & (N_STAB | N_EXT)))
  4747.         /* ordinary local symbol */
  4748.     write = ((discard_locals != DISCARD_ALL)
  4749.          && !(discard_locals == DISCARD_L &&
  4750.               (p->n_un.n_strx + entry->strings)[0] == LPREFIX)
  4751.          && type != N_WARNING);
  4752.       else if (!(type & N_EXT))
  4753.     /* debugger symbol */
  4754.         write = (strip_symbols == STRIP_NONE);
  4755.  
  4756.       if (write)
  4757.     {
  4758.       /* If this symbol has a name,
  4759.          allocate space for it in the output string table.  */
  4760.  
  4761.       if (p->n_un.n_strx)
  4762.         p->n_un.n_strx = assign_string_table_index (p->n_un.n_strx
  4763.                             + entry->strings);
  4764.  
  4765.       /* Output this symbol to the buffer and count it.  */
  4766.  
  4767.       *bufp++ = *p;
  4768.       (*syms_written_addr)++;
  4769.     }
  4770.     }
  4771.  
  4772.   /* All the symbols are now in BUF; write them.  */
  4773.  
  4774.   lseek (outdesc, symbol_table_offset + symbol_table_len, 0);
  4775. #if HOST_BYTE_ORDER != TARGET_BYTE_ORDER
  4776.   {
  4777.     int i;
  4778.  
  4779.     for (i = 0; i < bufp - buf; ++i)
  4780.       {
  4781.     fix_byte_order(&buf[i].n_un.n_name, sizeof(buf->n_un.n_name));
  4782.     fix_byte_order(&buf[i].n_desc, sizeof(buf->n_desc));
  4783.     fix_byte_order(&buf[i].n_value, sizeof(buf->n_value));
  4784.       }
  4785.   }
  4786. #endif
  4787.   mywrite (buf, sizeof (struct nlist), bufp - buf, outdesc);
  4788.   symbol_table_len += sizeof (struct nlist) * (bufp - buf);
  4789.  
  4790.   /* Write the string-table data for the symbols just written,
  4791.      using the data in vectors `strtab_vector' and `strtab_lens'.  */
  4792.  
  4793.   write_string_table ();
  4794.   entry->strings = 0;        /* Since it will dissapear anyway.  */
  4795. }
  4796.  
  4797. /* Copy any GDB symbol segments from the input files to the output file.
  4798.    The contents of the symbol segment is copied without change
  4799.    except that we store some information into the beginning of it.  */
  4800.  
  4801. void write_file_symseg ();
  4802.  
  4803. void
  4804. write_symsegs ()
  4805. {
  4806.   each_file (write_file_symseg, 0);
  4807. }
  4808.  
  4809. void
  4810. write_file_symseg (entry)
  4811.      struct file_entry *entry;
  4812. {
  4813.   char buffer[4096];
  4814.   struct symbol_root root;
  4815.   int indesc;
  4816.   int len;
  4817.  
  4818.   if (entry->symseg_offset == 0)
  4819.     return;
  4820.  
  4821.   /* This entry has a symbol segment.  Read the root of the segment.  */
  4822.  
  4823.   indesc = file_open (entry);
  4824.   lseek (indesc, entry->symseg_offset + entry->starting_offset, 0);
  4825.   if (sizeof root != read (indesc, &root, sizeof root))
  4826.     fatal_with_file ("premature end of file in symbol segment of ", entry);
  4827.  
  4828. #if HOST_BYTE_ORDER != TARGET_BYTE_ORDER
  4829.   fix_symbol_root_byte_order(&root);
  4830. #endif
  4831.  
  4832.   /* Store some relocation info into the root.  */
  4833.  
  4834.   root.ldsymoff = entry->local_syms_offset;
  4835.   root.textrel = entry->text_start_address;
  4836.   root.datarel = entry->data_start_address - entry->header.a_text;
  4837.   root.bssrel = entry->bss_start_address
  4838.     - entry->header.a_text - entry->header.a_data;
  4839.   root.databeg = entry->data_start_address - root.datarel;
  4840.   root.bssbeg = entry->bss_start_address - root.bssrel;
  4841.  
  4842.   /* Write the modified root into the output file.  */
  4843.  
  4844. #if HOST_BYTE_ORDER != TARGET_BYTE_ORDER
  4845.   fix_symbol_root_byte_order(&root);
  4846. #endif
  4847.   mywrite (&root, sizeof root, 1, outdesc);
  4848.  
  4849.   /* Copy the rest of the symbol segment unchanged.  */
  4850.  
  4851.   if (entry->superfile)
  4852.     {
  4853.       /* Library member: number of bytes to copy is determined
  4854.      from the member's total size.  */
  4855.  
  4856.       int total = entry->total_size - entry->symseg_offset - sizeof root;
  4857.  
  4858.       while (total > 0)
  4859.     {
  4860.       len = read (indesc, buffer, min (sizeof buffer, total));
  4861.  
  4862.       if (len != min (sizeof buffer, total))
  4863.         fatal_with_file ("premature end of file in symbol segment of ", entry);
  4864.       total -= len;
  4865.       mywrite (buffer, len, 1, outdesc);
  4866.     }
  4867.     }
  4868.   else
  4869.     {
  4870.       /* A separate file: copy until end of file.  */
  4871.  
  4872.       while (len = read (indesc, buffer, sizeof buffer))
  4873.     {
  4874.       mywrite (buffer, len, 1, outdesc);
  4875.       if (len < sizeof buffer)
  4876.         break;
  4877.     }
  4878.     }
  4879.  
  4880.   file_close ();
  4881. }
  4882.  
  4883. /* Create the symbol table entries for `etext', `edata' and `end'.  */
  4884.  
  4885. void
  4886. symtab_init ()
  4887. {
  4888. #ifndef nounderscore
  4889.   edata_symbol = getsym ("_edata");
  4890.   etext_symbol = getsym ("_etext");
  4891.   end_symbol = getsym ("_end");
  4892. #else
  4893.   edata_symbol = getsym ("edata");
  4894.   etext_symbol = getsym ("etext");
  4895.   end_symbol = getsym ("end");
  4896. #endif
  4897.  
  4898. #if TARGET_MACHINE==TARGET_SUN4 || TARGET_MACHINE==TARGET_SUN3 || TARGET_MACHINE==TARGET_SUN2
  4899.   {
  4900.     symbol *dynamic_symbol = getsym ("__DYNAMIC");
  4901.     dynamic_symbol->defined = N_ABS | N_EXT;
  4902.     dynamic_symbol->referenced = 1;
  4903.     dynamic_symbol->value = 0;
  4904.   }
  4905. #endif
  4906. #if TARGET_MACHINE==TARGET_SEQUENT
  4907.   {
  4908.     symbol *_387_flt_symbol = getsym ("_387_flt");
  4909.     _387_flt_symbol->defined = N_ABS | N_EXT;
  4910.     _387_flt_symbol->referenced = 1;
  4911.     _387_flt_symbol->value = 0;
  4912.   }
  4913. #endif
  4914.  
  4915.   edata_symbol->defined = N_DATA | N_EXT;
  4916.   etext_symbol->defined = N_TEXT | N_EXT;
  4917.   end_symbol->defined = N_BSS | N_EXT;
  4918.  
  4919.   edata_symbol->referenced = 1;
  4920.   etext_symbol->referenced = 1;
  4921.   end_symbol->referenced = 1;
  4922. }
  4923.  
  4924. /* Compute the hash code for symbol name KEY.  */
  4925.  
  4926. int
  4927. hash_string (key)
  4928.      char *key;
  4929. {
  4930.   register char *cp;
  4931.   register int k;
  4932.  
  4933.   cp = key;
  4934.   k = 0;
  4935.   while (*cp)
  4936.     k = (((k << 1) + (k >> 14)) ^ (*cp++)) & 0x3fff;
  4937.  
  4938.   return k;
  4939. }
  4940.  
  4941. /* Get the symbol table entry for the global symbol named KEY.
  4942.    Create one if there is none.  */
  4943.  
  4944. symbol *
  4945. getsym (key)
  4946.      char *key;
  4947. {
  4948.   register int hashval;
  4949.   register symbol *bp;
  4950.  
  4951.   /* Determine the proper bucket.  */
  4952.  
  4953.   hashval = hash_string (key) % TABSIZE;
  4954.  
  4955.   /* Search the bucket.  */
  4956.  
  4957.   for (bp = symtab[hashval]; bp; bp = bp->link)
  4958.     if (! strcmp (key, bp->name))
  4959.       return bp;
  4960.  
  4961.   /* Nothing was found; create a new symbol table entry.  */
  4962.  
  4963.   bp = (symbol *) xmalloc (sizeof (symbol));
  4964.   bp->refs = 0;
  4965.   bp->name = (char *) xmalloc (strlen (key) + 1);
  4966.   strcpy (bp->name, key);
  4967.   bp->defined = 0;
  4968.   bp->referenced = 0;
  4969.   bp->trace = 0;
  4970.   bp->value = 0;
  4971.   bp->max_common_size = 0;
  4972.   bp->warning = 0;
  4973.   bp->undef_refs = 0;
  4974.   bp->multiply_defined = 0;
  4975.  
  4976.   /* Add the entry to the bucket.  */
  4977.  
  4978.   bp->link = symtab[hashval];
  4979.   symtab[hashval] = bp;
  4980.  
  4981.   ++num_hash_tab_syms;
  4982.  
  4983.   return bp;
  4984. }
  4985.  
  4986. /* Like `getsym' but return 0 if the symbol is not already known.  */
  4987.  
  4988. symbol *
  4989. getsym_soft (key)
  4990.      char *key;
  4991. {
  4992.   register int hashval;
  4993.   register symbol *bp;
  4994.  
  4995.   /* Determine which bucket.  */
  4996.  
  4997.   hashval = hash_string (key) % TABSIZE;
  4998.  
  4999.   /* Search the bucket.  */
  5000.  
  5001.   for (bp = symtab[hashval]; bp; bp = bp->link)
  5002.     if (! strcmp (key, bp->name))
  5003.       return bp;
  5004.  
  5005.   return 0;
  5006. }
  5007.  
  5008. /* Report a fatal error.
  5009.    STRING is a printf format string and ARG is one arg for it.  */
  5010.  
  5011. void
  5012. fatal (string, arg)
  5013.      char *string, *arg;
  5014. {
  5015.   fprintf (stderr, "ld: ");
  5016.   fprintf (stderr, string, arg);
  5017.   fprintf (stderr, "\n");
  5018.   exit (1);
  5019. }
  5020.  
  5021. /* Report a fatal error.  The error message is STRING
  5022.    followed by the filename of ENTRY.  */
  5023.  
  5024. void
  5025. fatal_with_file (string, entry)
  5026.      char *string;
  5027.      struct file_entry *entry;
  5028. {
  5029.   fprintf (stderr, "ld: ");
  5030.   fprintf (stderr, string);
  5031.   print_file_name (entry, stderr);
  5032.   fprintf (stderr, "\n");
  5033.   exit (1);
  5034. }
  5035.  
  5036. /* Report a fatal error using the message for the last failed system call,
  5037.    followed by the string NAME.  */
  5038.  
  5039. void
  5040. perror_name (name)
  5041.      char *name;
  5042. {
  5043.   extern int errno, sys_nerr;
  5044.   extern char *sys_errlist[];
  5045.   char *s;
  5046.  
  5047.   if (errno < sys_nerr)
  5048.     s = concat ("", sys_errlist[errno], " for %s");
  5049.   else
  5050.     s = "cannot open %s";
  5051.   fatal (s, name);
  5052. }
  5053.  
  5054. /* Report a fatal error using the message for the last failed system call,
  5055.    followed by the name of file ENTRY.  */
  5056.  
  5057. void
  5058. perror_file (entry)
  5059.      struct file_entry *entry;
  5060. {
  5061.   extern int errno, sys_nerr;
  5062.   extern char *sys_errlist[];
  5063.   char *s;
  5064.  
  5065.   if (errno < sys_nerr)
  5066.     s = concat ("", sys_errlist[errno], " for ");
  5067.   else
  5068.     s = "cannot open ";
  5069.   fatal_with_file (s, entry);
  5070. }
  5071.  
  5072. /* Report a nonfatal error.
  5073.    STRING is a format for printf, and ARG1 ... ARG3 are args for it.  */
  5074.  
  5075. void
  5076. error (string, arg1, arg2, arg3)
  5077.      char *string, *arg1, *arg2, *arg3;
  5078. {
  5079.   fprintf (stderr, "%s: ", progname);
  5080.   fprintf (stderr, string, arg1, arg2, arg3);
  5081.   fprintf (stderr, "\n");
  5082. }
  5083.  
  5084.  
  5085. /* Output COUNT*ELTSIZE bytes of data at BUF
  5086.    to the descriptor DESC.  */
  5087.  
  5088. void
  5089. mywrite (buf, count, eltsize, desc)
  5090.      char *buf;
  5091.      int count;
  5092.      int eltsize;
  5093.      int desc;
  5094. {
  5095.   register int val;
  5096.   register int bytes = count * eltsize;
  5097.  
  5098.   while (bytes > 0)
  5099.     {
  5100.       val = write (desc, buf, bytes);
  5101.       if (val <= 0)
  5102.     perror_name (output_filename);
  5103.       buf += val;
  5104.       bytes -= val;
  5105.     }
  5106. }
  5107.  
  5108. /* Output PADDING zero-bytes to descriptor OUTDESC.
  5109.    PADDING may be negative; in that case, do nothing.  */
  5110.  
  5111. void
  5112. padfile (padding, outdesc)
  5113.      int padding;
  5114.      int outdesc;
  5115. {
  5116.   register char *buf;
  5117.   if (padding <= 0)
  5118.     return;
  5119.  
  5120.   buf = (char *) alloca (padding);
  5121.   bzero (buf, padding);
  5122.   mywrite (buf, padding, 1, outdesc);
  5123. }
  5124.  
  5125. /* Return a newly-allocated string
  5126.    whose contents concatenate the strings S1, S2, S3.  */
  5127.  
  5128. char *
  5129. concat (s1, s2, s3)
  5130.      char *s1, *s2, *s3;
  5131. {
  5132.   register int len1 = strlen (s1), len2 = strlen (s2), len3 = strlen (s3);
  5133.   register char *result = (char *) xmalloc (len1 + len2 + len3 + 1);
  5134.  
  5135.   strcpy (result, s1);
  5136.   strcpy (result + len1, s2);
  5137.   strcpy (result + len1 + len2, s3);
  5138.   result[len1 + len2 + len3] = 0;
  5139.  
  5140.   return result;
  5141. }
  5142.  
  5143. /* Parse the string ARG using scanf format FORMAT, and return the result.
  5144.    If it does not parse, report fatal error
  5145.    generating the error message using format string ERROR and ARG as arg.  */
  5146.  
  5147. int
  5148. parse (arg, format, error)
  5149.      char *arg, *format;
  5150. {
  5151.   int x;
  5152.   if (1 != sscanf (arg, format, &x))
  5153.     fatal (error, arg);
  5154.   return x;
  5155. }
  5156.  
  5157. /* Like malloc but get fatal error if memory is exhausted.  */
  5158.  
  5159. int
  5160. xmalloc (size)
  5161.      int size;
  5162. {
  5163.   register int result = malloc (size);
  5164.   if (!result)
  5165.     fatal ("virtual memory exhausted", 0);
  5166.   return result;
  5167. }
  5168.  
  5169. /* Like realloc but get fatal error if memory is exhausted.  */
  5170.  
  5171. int
  5172. xrealloc (ptr, size)
  5173.      char *ptr;
  5174.      int size;
  5175. {
  5176.   register int result = realloc (ptr, size);
  5177.   if (!result)
  5178.     fatal ("virtual memory exhausted", 0);
  5179.   return result;
  5180. }
  5181.  
  5182. #ifdef USG
  5183.  
  5184. void
  5185. bzero (p, n)
  5186.      char *p;
  5187. {
  5188.   memset (p, 0, n);
  5189. }
  5190.  
  5191. void
  5192. bcopy (from, to, n)
  5193.      char *from, *to;
  5194. {
  5195.   memcpy (to, from, n);
  5196. }
  5197.  
  5198. getpagesize ()
  5199. {
  5200.   return (4096);
  5201. }
  5202.  
  5203. #endif
  5204.  
  5205.  
  5206. #if HOST_BYTE_ORDER != TARGET_BYTE_ORDER
  5207.  
  5208. static void
  5209. fix_byte_order(p, n)
  5210.     char *p;
  5211.     int n;
  5212. {
  5213.     char t;
  5214.  
  5215.     switch (n) {
  5216.  
  5217.     case 1:
  5218.     return;
  5219.  
  5220.     case 2:
  5221.     t = p[0];
  5222.     p[0] = p[1];
  5223.     p[1] = t;
  5224.     return;
  5225.  
  5226.     case 4:
  5227.     t = p[0];
  5228.     p[0] = p[3];
  5229.     p[3] = t;
  5230.     t = p[1];
  5231.     p[1] = p[2];
  5232.     p[2] = t;
  5233.     return;
  5234.  
  5235.     default:
  5236.     fatal("Internal Error in fix_byte_order, n = %d\n", 0);
  5237.     }
  5238. }
  5239.  
  5240. static void
  5241. fix_exec_header_byte_order(execP)
  5242.     struct exec *execP;
  5243. {
  5244.  
  5245. #if TARGET_MACHINE==TARGET_SUN4
  5246.     if (N_BADMAG(*execP)) {
  5247.     u_char c = * (u_char *) execP;
  5248.     execP->a_dynamic = c & 1;
  5249.     execP->a_toolversion = c >> 1;
  5250.     } else {
  5251.     u_char c = execP->a_toolversion | (execP->a_dynamic << 7);
  5252.     * (u_char *) execP = c;
  5253.     }
  5254. #elif TARGET_MACHINE!=TARGET_SEQUENT
  5255.     fix_byte_order(&execP->a_machtype, sizeof(execP->a_machtype));
  5256. #endif
  5257.     fix_byte_order(&execP->a_magic,    sizeof(execP->a_magic));
  5258.     fix_byte_order(&execP->a_text,     sizeof(execP->a_text));
  5259.     fix_byte_order(&execP->a_data,     sizeof(execP->a_data));
  5260.     fix_byte_order(&execP->a_bss,      sizeof(execP->a_bss));
  5261.     fix_byte_order(&execP->a_syms,     sizeof(execP->a_syms));
  5262.     fix_byte_order(&execP->a_entry,    sizeof(execP->a_entry));
  5263.     fix_byte_order(&execP->a_trsize,   sizeof(execP->a_trsize));
  5264.     fix_byte_order(&execP->a_drsize,   sizeof(execP->a_drsize));
  5265.     return;
  5266. }
  5267.  
  5268. static void
  5269. fix_symbol_byte_order(p, size)
  5270.     struct nlist *p;
  5271.     int size;
  5272. {
  5273.     int n;
  5274.  
  5275.     for (n = size / sizeof(*p); --n >= 0; ++p) {
  5276.     fix_byte_order(&p->n_un, sizeof(p->n_un));
  5277.     fix_byte_order(&p->n_desc, sizeof(p->n_desc));
  5278.     fix_byte_order(&p->n_value, sizeof(p->n_value));
  5279.     }
  5280.     return;
  5281. }
  5282.  
  5283. static void
  5284. target_to_host_reloc_byte_order(relocP, nrelocs)
  5285.     struct relocation_info *relocP;
  5286.     int nrelocs;
  5287. {
  5288.     u_char c[4];
  5289.  
  5290. #if TARGET_BYTE_ORDER==BIG_ENDIAN && HOST_BYTE_ORDER==LITTLE_ENDIAN
  5291.     for (; --nrelocs >= 0; ++relocP) {
  5292.     fix_byte_order(&relocP->r_address, sizeof(relocP->r_address));
  5293.     c[0] = ((u_char *) relocP)[4];
  5294.     c[1] = ((u_char *) relocP)[5];
  5295.     c[2] = ((u_char *) relocP)[6];
  5296.     c[3] = ((u_char *) relocP)[7];
  5297.     ((u_long *) relocP)[1] = 0;
  5298. #if TARGET_MACHINE==TARGET_SUN4
  5299.     relocP->r_index = (c[0] << 16) | (c[1] << 8) | c[2];
  5300.     relocP->r_type = (enum reloc_type) (c[3] & 0x1f);
  5301.     relocP->r_extern = (c[3] & 0x80) >> 7;
  5302.     fix_byte_order(&relocP->r_addend, sizeof(relocP->r_addend));
  5303. #else
  5304.     relocP->r_symbolnum = (c[0] << 16) | (c[1] << 8) | c[2];
  5305.     relocP->r_pcrel = (c[3] >> 7) & 1;
  5306.     relocP->r_length = (c[3] >> 5) & 3;
  5307.     relocP->r_extern = (c[3] >> 4) & 1;
  5308. #endif
  5309.     }
  5310. #endif
  5311.     return;
  5312. }
  5313.  
  5314. static void
  5315. host_to_target_reloc_byte_order(relocP, nrelocs)
  5316.     struct relocation_info *relocP;
  5317.     int nrelocs;
  5318. {
  5319.     u_char c[4];
  5320.  
  5321. #if TARGET_BYTE_ORDER==BIG_ENDIAN && HOST_BYTE_ORDER==LITTLE_ENDIAN
  5322.     for (; --nrelocs >= 0; ++relocP) {
  5323.     fix_byte_order(&relocP->r_address, sizeof(relocP->r_address));
  5324. #if TARGET_MACHINE==TARGET_SUN4
  5325.     c[0] = (relocP->r_index >> 16);
  5326.     c[1] = (relocP->r_index >> 8);
  5327.     c[2] = relocP->r_index;
  5328.     c[3] = (u_char) relocP->r_type | (relocP->r_extern << 7);
  5329.     fix_byte_order(&relocP->r_addend, sizeof(relocP->r_addend));
  5330. #else
  5331.     c[0] = (relocP->r_symbolnum >> 16);
  5332.     c[1] = (relocP->r_symbolnum >> 8);
  5333.     c[2] = relocP->r_symbolnum;
  5334.     c[3] = (relocP->r_pcrel << 7) |
  5335.         (relocP->r_length << 5) | (relocP->r_extern << 4);
  5336. #endif
  5337.     ((u_char *) relocP)[4] = c[0];
  5338.     ((u_char *) relocP)[5] = c[1];
  5339.     ((u_char *) relocP)[6] = c[2];
  5340.     ((u_char *) relocP)[7] = c[3];
  5341.     }
  5342. #endif
  5343.     return;
  5344. }
  5345.  
  5346. static void
  5347. fix_symbol_root_byte_order(p)
  5348.     struct symbol_root *p;
  5349. {
  5350.  
  5351.     fix_byte_order(&p->format, sizeof(p->format));
  5352.     fix_byte_order(&p->length, sizeof(p->length));
  5353.     fix_byte_order(&p->ldsymoff, sizeof(p->ldsymoff));
  5354.     fix_byte_order(&p->textrel, sizeof(p->textrel));
  5355.     fix_byte_order(&p->datarel, sizeof(p->datarel));
  5356.     fix_byte_order(&p->bssrel, sizeof(p->bssrel));
  5357.     fix_byte_order(&p->filename, sizeof(p->filename));
  5358.     fix_byte_order(&p->filedir, sizeof(p->filedir));
  5359.     fix_byte_order(&p->blockvector, sizeof(p->blockvector));
  5360.     fix_byte_order(&p->typevector, sizeof(p->typevector));
  5361.     fix_byte_order(&p->language, sizeof(p->language));
  5362.     fix_byte_order(&p->version, sizeof(p->version));
  5363.     fix_byte_order(&p->compilation, sizeof(p->compilation));
  5364.     fix_byte_order(&p->databeg, sizeof(p->databeg));
  5365.     fix_byte_order(&p->bssbeg, sizeof(p->bssbeg));
  5366.     fix_byte_order(&p->sourcevector, sizeof(p->sourcevector));
  5367.     return;
  5368. }
  5369.  
  5370. #endif
  5371.  
  5372. @
  5373.  
  5374.  
  5375. 1.5
  5376. log
  5377. @Changes for sequent.
  5378. @
  5379. text
  5380. @a29 1
  5381. #undef sym   
  5382. d150 1
  5383. a150 1
  5384. #include <sym.md/a.out.h>
  5385. d4114 1
  5386. a4114 1
  5387.           if ((int)p->r_type <= RELOC_32
  5388. @
  5389.  
  5390.  
  5391. 1.4
  5392. log
  5393. @*** empty log message ***
  5394. @
  5395. text
  5396. @d30 3
  5397. a32 1
  5398. #define TARGET_VAX              1
  5399. d40 2
  5400. d104 2
  5401. a105 1
  5402.     TARGET_MACHINE==TARGET_I386
  5403. d128 2
  5404. a129 1
  5405.     defined(ds3100)     
  5406. d150 4
  5407. d239 1
  5408. a239 1
  5409. #if TARGET_MAHCINE==TARGET_SEQUENT
  5410. d290 21
  5411. d456 1
  5412. d686 9
  5413. d969 6
  5414. d1073 12
  5415. d1120 1
  5416. d1137 2
  5417. a1138 2
  5418.   cmdline_references
  5419.     = (struct glosym **) xmalloc (cl_refs_allocated
  5420. d1173 3
  5421. d1238 1
  5422. d1255 9
  5423. d1364 5
  5424. a1368 1
  5425.   if ((magic == ZMAGIC)
  5426. d1491 10
  5427. d1502 5
  5428. d1601 7
  5429. a1732 1
  5430.       register char **p = search_dirs;
  5431. a1982 1
  5432.   int lowest_set_vector = -1;
  5433. d2103 2
  5434. a2104 2
  5435.       nlist_p->n_value
  5436.         = (unsigned int) getsym ((nlist_p + 1)->n_un.n_strx
  5437. a2593 1
  5438. #ifdef foobar          
  5439. d2595 1
  5440. a2595 3
  5441.       && !set_element_prefixed_p (name)
  5442. #endif          
  5443.           )
  5444. d2623 1
  5445. a2623 4
  5446. #ifdef foobar          
  5447.           || (sp->defined && sp->max_common_size)
  5448. #endif
  5449.           )
  5450. a2633 1
  5451. #if foobar
  5452. a2637 1
  5453. #endif
  5454. d2646 27
  5455. d2713 4
  5456. a2716 1
  5457. #else
  5458. d2718 2
  5459. a2719 1
  5460. #endif
  5461. d2740 4
  5462. d2752 2
  5463. a2753 2
  5464.       set_sect_size
  5465.     = (2 * set_symbol_count + set_vector_count) * sizeof (unsigned long);
  5466. d2784 1
  5467. a2784 1
  5468.       int defs = 0, com = sp->max_common_size, erred = 0;
  5469. d2866 2
  5470. a2867 2
  5471.           unsigned long length_word_index
  5472.         = (sp->value - set_sect_start) / sizeof (unsigned long);
  5473. d2870 2
  5474. a2871 3
  5475.           set_vectors[length_word_index]
  5476.         = setv_fill_count - 1 - length_word_index;
  5477. #ifdef foobar
  5478. d2878 2
  5479. a2879 2
  5480.           set_vectors[length_word_index + i]
  5481.             = set_vectors[setv_fill_count - i];
  5482. d2881 1
  5483. a2881 1
  5484. #endif        }
  5485. d2908 3
  5486. d2912 1
  5487. d2914 1
  5488. a2914 1
  5489.                - data_size;
  5490. d3084 1
  5491. a3084 1
  5492.   struct nlist *sym;
  5493. d3120 1
  5494. a3120 1
  5495.   struct file_entry *entry = (struct file_entry *) source->sym;
  5496. d3122 1
  5497. a3122 1
  5498.   current->sym = next->sym;
  5499. d3126 1
  5500. a3126 1
  5501.   while (++(next->sym) < (entry->symbols
  5502. d3132 1
  5503. a3132 1
  5504.       switch (next->sym->n_type & 0xff) 
  5505. d3136 1
  5506. a3136 1
  5507.       next->line = next->sym->n_desc;
  5508. d3140 1
  5509. a3140 1
  5510.       next->line = next->sym->n_desc;
  5511. d3148 1
  5512. a3148 1
  5513.       source->filename = next->sym->n_un.n_strx + entry->strings;
  5514. d3154 2
  5515. a3155 2
  5516.       next->filename
  5517.         = next->sym->n_un.n_strx + entry->strings;
  5518. d3160 1
  5519. a3160 1
  5520.   next->sym = (struct nlist *) 0;
  5521. d3175 3
  5522. a3177 3
  5523.     *state_pointer
  5524.       = (struct line_debug_entry *)
  5525.     xmalloc (3 * sizeof (struct line_debug_entry));
  5526. d3198 1
  5527. a3198 1
  5528.       current->sym = next->sym = (struct nlist *) 0;
  5529. d3205 2
  5530. a3206 2
  5531.   source->sym = (struct nlist *) entry;
  5532.   next->sym = tmp;
  5533. d3210 1
  5534. a3210 1
  5535.   if (!next->sym)        /* No line numbers for this section; */
  5536. d3250 2
  5537. a3251 2
  5538.   if (next->sym)
  5539.     use_data_symbols = (next->sym->n_type & N_TYPE) == N_DATA;
  5540. d3256 1
  5541. a3256 1
  5542.   if (current->sym->n_value > address)
  5543. d3260 1
  5544. a3260 1
  5545.                      ((state_pointer + 2)->sym));
  5546. d3268 1
  5547. a3268 1
  5548.   if (current->sym->n_value > address)
  5549. d3271 2
  5550. a3272 2
  5551.   while (next->sym
  5552.      && next->sym->n_value <= address
  5553. d3302 5
  5554. a3306 5
  5555.   int reloc_size
  5556.     = ((data_segment ? entry->header.a_drsize : entry->header.a_trsize)
  5557.        / sizeof (struct relocation_info));
  5558.   int start_of_segment
  5559.     = (data_segment ? entry->data_start_address : entry->text_start_address);
  5560. d3308 2
  5561. a3309 2
  5562.   struct line_debug_entry *state_pointer
  5563.     = init_debug_scan (data_segment != 0, entry);
  5564. d3311 1
  5565. a3311 3
  5566.     *current = state_pointer,
  5567.     *next = state_pointer + 1,
  5568.     *source = state_pointer + 2;
  5569. d3389 8
  5570. a3396 2
  5571.       errmsg = (char *) xmalloc (strlen (errfmt) + strlen (g->name) + 1);
  5572.       sprintf (errmsg, errfmt, g->name, data_segment ? "data" : "text");
  5573. d3425 2
  5574. a3426 2
  5575.   unsigned char *nlist_bitvector
  5576.     = (unsigned char *) alloca ((number_of_syms >> 3) + 1);
  5577. d3504 1
  5578. a3504 1
  5579.       
  5580. d3527 1
  5581. a3527 1
  5582.       
  5583. d3534 1
  5584. a3534 1
  5585.     fprintf (outfile, "%s", errfmt);
  5586. d3536 9
  5587. a3544 1
  5588.     fprintf (outfile, errfmt, g->name);
  5589. d3569 3
  5590. a3571 1
  5591.  
  5592. d3624 2
  5593. d3628 2
  5594. d3697 18
  5595. d3769 5
  5596. d3889 5
  5597. d4093 1
  5598. a4093 1
  5599.  
  5600. d4096 1
  5601. d4112 1
  5602. a4112 1
  5603. #if 0
  5604. d4125 4
  5605. d4667 3
  5606. d4671 1
  5607. d5207 1
  5608. a5207 1
  5609. #else
  5610. @
  5611.  
  5612.  
  5613. 1.3
  5614. log
  5615. @Fixed magic number for -T option.  (Different on sprite than unix).
  5616. @
  5617. text
  5618. @d104 4
  5619. d139 7
  5620. d148 1
  5621. d406 4
  5622. d1020 1
  5623. a1020 1
  5624.  
  5625. d1026 4
  5626. d1031 2
  5627. d1552 1
  5628. a1552 1
  5629.  
  5630. d1577 1
  5631. a1894 1
  5632.  
  5633. a1953 1
  5634.  
  5635. d2136 1
  5636. d2148 1
  5637. d2339 4
  5638. a2342 1
  5639.                || (sp->defined && sp->max_common_size)))
  5640. a2471 1
  5641.  
  5642. d2492 1
  5643. a2492 1
  5644.           
  5645. d2497 1
  5646. d2499 3
  5647. a2501 1
  5648.       && !set_element_prefixed_p (name))
  5649. d2529 4
  5650. a2532 1
  5651.           || (sp->defined && sp->max_common_size))
  5652. d2543 1
  5653. a2543 1
  5654.  
  5655. d2548 1
  5656. a2548 1
  5657.  
  5658. d2583 1
  5659. a2583 1
  5660.   int setv_fill_count;
  5661. d2615 1
  5662. a2615 1
  5663.  
  5664. d2619 1
  5665. a2619 1
  5666.  
  5667. d2633 1
  5668. d2635 1
  5669. d2748 1
  5670. a2748 1
  5671.  
  5672. d2758 1
  5673. a2758 1
  5674.         }
  5675. d2767 1
  5676. d2771 1
  5677. @
  5678.  
  5679.  
  5680. 1.2
  5681. log
  5682. @Fixed byte order problems.  These changes have been sent to gnu.
  5683. @
  5684. text
  5685. @d231 1
  5686. a231 1
  5687.     (((x).a_magic==Omagic)? (N_TXTADDR(x)+(x).a_text) \
  5688. d1265 1
  5689. d1273 1
  5690. d1349 3
  5691. d1435 3
  5692. @
  5693.  
  5694.  
  5695. 1.1
  5696. log
  5697. @Initial revision
  5698. @
  5699. text
  5700. @d30 102
  5701. d138 10
  5702. d200 1
  5703. a200 1
  5704. #ifdef hpux
  5705. d211 1
  5706. a211 1
  5707. #if defined(vax) || defined(sony_news)
  5708. d214 1
  5709. a214 1
  5710. #ifdef is68k
  5711. d217 1
  5712. a217 1
  5713. #ifdef sequent
  5714. d223 1
  5715. a223 1
  5716. #if defined(vax) || defined(sony_news)
  5717. d228 1
  5718. a228 1
  5719. #ifdef is68k
  5720. d234 1
  5721. a234 1
  5722. #ifdef sequent
  5723. d242 1
  5724. a242 2
  5725. #ifdef sun
  5726. #ifdef sparc
  5727. d246 1
  5728. a246 1
  5729. #if defined(mc68010) || defined(m68010)
  5730. d249 1
  5731. a249 1
  5732. #ifndef INITIALIZE_HEADER
  5733. d252 1
  5734. a252 2
  5735. #endif
  5736. #ifdef ALTOS
  5737. d255 1
  5738. a255 9
  5739. #ifdef is68k
  5740. #ifdef M_68020
  5741. /* ISI rel 4.0D doesn't use it, and rel 3.05 doesn't have an
  5742.    a_machtype field and so won't recognize the magic number.  To keep
  5743.    binary compatibility for now, just ignore it */
  5744. #define INITIALIZE_HEADER outheader.a_machtype = 0;
  5745. #endif
  5746. #endif
  5747. #ifdef hpux
  5748. d258 1
  5749. a258 1
  5750. #if defined(i386) && !defined(sequent)
  5751. d261 2
  5752. a262 2
  5753.  
  5754. #ifdef is68k
  5755. d358 1
  5756. a358 1
  5757. #if defined(sun) && defined(sparc)
  5758. d369 6
  5759. a374 4
  5760. #define RELOC_PCREL_P(r)             \
  5761.         ((r)->r_type >= RELOC_DISP8 && (r)->r_type <= RELOC_WDISP22)
  5762. #define RELOC_VALUE_RIGHTSHIFT(r)       (reloc_target_rightshift[(r)->r_type])
  5763. #define RELOC_TARGET_SIZE(r)            (reloc_target_size[(r)->r_type])
  5764. d376 2
  5765. a377 1
  5766. #define RELOC_TARGET_BITSIZE(r)         (reloc_target_bitsize[(r)->r_type])
  5767. d396 1
  5768. a396 1
  5769. #ifdef sequent
  5770. a1013 1
  5771.  
  5772. d1703 3
  5773. d1732 3
  5774. a1734 1
  5775.  
  5776. d1738 3
  5777. a1740 1
  5778.  
  5779. d1820 3
  5780. a1822 1
  5781.  
  5782. d2077 1
  5783. a2077 1
  5784. #ifdef sequent
  5785. d2246 3
  5786. a2248 1
  5787.  
  5788. d2255 8
  5789. d2568 1
  5790. a2568 1
  5791. #ifdef sequent
  5792. d3440 1
  5793. a3440 1
  5794. #ifdef sequent
  5795. d3549 3
  5796. d3553 3
  5797. d3621 4
  5798. d3638 4
  5799. d3681 4
  5800. d3719 9
  5801. d3772 4
  5802. d3819 1
  5803. d3932 8
  5804. d3941 1
  5805. a3941 1
  5806.         relocation -= mask & *(short *) (data + addr);
  5807. d3943 11
  5808. a3953 3
  5809.         relocation += mask & *(short *) (data + addr);
  5810.       *(short *) (data + addr) &= ~mask;
  5811.       *(short *) (data + addr) |= relocation;
  5812. d3957 14
  5813. a3970 2
  5814.       if (RELOC_MEMORY_SUB_P(p))
  5815.         relocation -= mask & *(long *) (data + addr);
  5816. d3972 15
  5817. a3986 3
  5818.         relocation += mask & *(long *) (data + addr);
  5819.       *(long *) (data + addr) &= ~mask;
  5820.       *(long *) (data + addr) |= relocation;
  5821. d4101 4
  5822. d4164 4
  5823. d4375 12
  5824. d4399 4
  5825. d4503 12
  5826. d4556 4
  5827. d4572 3
  5828. d4626 1
  5829. a4626 1
  5830. #ifdef sun
  5831. d4634 1
  5832. a4634 1
  5833. #ifdef sequent
  5834. d4932 168
  5835. @
  5836.